0

im trying to pass a jquery variable to php this is the code

<label>Turnamen</label>
<div class="input-group">
   <div class="input-group-prepend">
      <span class="input-group-text">
         <input id="checkturnamen" type="checkbox">
      </span>
   </div>
   <select id="colturnamen" name="colturnamen" class="form-control" disabled="">
      @foreach ($nav as $navigasi)
         @if ($turnamen == $navigasi->waktu)
            <option value="terpilih" selected>{{$navigasi->waktu_text}}</option>
         @else
            <option>{{$navigasi->waktu_text}}</option>
         @endif
      @endforeach
   </select>
</div>

<label>Grup</label>
<div class="input-group">
    <div class="input-group-prepend">
        <span class="input-group-text">
           <input id="checkgrup" type="checkbox">
        </span>
    </div>
    <select id="colgrup" class="form-control" disabled="">
        <option value="terpilih" selected>NULL</option>
    </select>
</div>



<script>
   $(document).ready(function(){
      $('#colturnamen').change(function(){
          var pilihturnamen =  'February 2019';
          $('#colgrup').append('<?php $pilihturnamen ="'+pilihturnamen+'";echo($pilihturnamen); foreach($nav as $navigasi){if($pilihturnamen == $navigasi->waktu_text){if (is_null($navigasi->jumlah_grup)){}else{echo ("<option class="."autogrup".">A</option>");$alphabet = "A";for ($i = 1; $i < $navigasi->jumlah_grup; $i++){$alphabet++;echo ("<option class="."autogrup".">".$alphabet."</option>");}}}}?>');
          });
 </script>

if i run the code it show this

   <select id="colgrup" class="form-control">
       <option value="terpilih" selected="">NULL</option>
            February 2019
   </select>

then i change the script to this (i insert string manually)

<script>
       $(document).ready(function(){
          $('#colturnamen').change(function(){
              var pilihturnamen =  'February 2019';
              $('#colgrup').append('<?php $pilihturnamen ="February 2019";echo($pilihturnamen); foreach($nav as $navigasi){if($pilihturnamen == $navigasi->waktu_text){if (is_null($navigasi->jumlah_grup)){}else{echo ("<option class="."autogrup".">A</option>");$alphabet = "A";for ($i = 1; $i < $navigasi->jumlah_grup; $i++){$alphabet++;echo ("<option class="."autogrup".">".$alphabet."</option>");}}}}?>');
              });
 </script>

and i run it again it works

<select id="colgrup" class="form-control">
   <option value="terpilih" selected="">NULL</option>
   February 2019
  <option class="autogrup">A</option>
  <option class="autogrup">B</option>
  <option class="autogrup">C</option>
  <option class="autogrup">D</option>
  <option class="autogrup">E</option>
</select>

The Question is Why i can't pass a text inside jquery variable to that php code??

Wah Yudi
  • 3
  • 2
  • That's not how php works unfortunately. Php doesn't know what's happening in javascript land :( – Luke Feb 11 '20 at 14:59
  • What are you trying to achieve in this code? – ROOT Feb 11 '20 at 15:00
  • if you want something to be processed by PHP while JavaScript is running in the browser, you have to send that data to the server using AJAX (so that PHP can execute on the server) and then wait for the result to come back before continuing to execute your Javascript code. – ADyson Feb 11 '20 at 15:00
  • haha im trying to add new option list inside that "colgrup" that depends from what user select on "colturnamen". the point is every time "colturnamen" change they delete all previus option on "colgrup" and add new option – Wah Yudi Feb 11 '20 at 15:04

1 Answers1

0

On page request: The process flow looks like this:

  1. Server Side PHP code rendering

  2. Response sent to the Browser

  3. Browser begins rendering and executing JS

you would want to do an AJAX call and on "success" then execute your JS code.

Here are examples of AJAX call using jQuery:

$.ajax({
  type: "GET",
  url: url,
  success: success,
  ...
});
Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
  • i think i should use the GET or POST method then. im just to lazy to learn that thing. so i just using shortcut and try to make improve and it fails :) – Wah Yudi Feb 11 '20 at 15:19