I need to pass a js variable to a php variable. I know this can't be done directly and I have been trying to do it with ajax but I just can't figure it out.

  <form name="menu">
  <div>
    <select name="list1" size="1" onchange="setOptions(document.menu.list1.options
    [document.menu.list1.selectedIndex].value,document.menu.list2,document.menu.list3);">
    <option value=" " selected></option>
      <option value="one">item1</option>
      <option value="two">item2</option>
      <option value="three">item3</option>
      <option value="four">item4</option>
    </select><br><br>
    <select name="list2" size="1"onchange="setOptions(document.menu.list2.options
    [document.menu.list2.selectedIndex].value,document.menu.list3,' ');">
      <option value=" " selected>Select an option</option>
    </select><br><br>
    <select name="list3" size="1">
      <option value=" " selected>Select an option</option>
    </select><br>
  </div>
  </form>


  <script>
    function setOptions(chosen, selbox, selbox2) 
    {
    selbox.options.length = 0;

    if (chosen == " ")
    {
      selbox.options[selbox.options.length] = new Option('Select an option',' ');
      selbox2.options[selbox2.options.length] = new Option("Select an option"," ");
      setTimeout(setOptions(' ',document.menu.list3),5);
    }

       if (chosen == "one")
       {
         showList2(selbox, chosen);
       }
       if (chosen == "two")
       {
         showList2(selbox, chosen);
       }
       if (chosen == "three")
       {
         showList2(selbox, chosen);
       }
       if (chosen == "four")
       {
         showList2(selbox, chosen);
       }

       //some code snipped
     }


    function showList2(selbox, chosen)
    {
    <?php
    $n = 0;
    $list1 = chosen;  //where i need to pass the variable

      foreach($list[$list1] as $key => $value)
      {
        $key_array[$n] =  $key;
        $Lname[$n] = $list[$list1][$key]['name'];

      $n++;
      }

    $jsArray = json_encode($key_array);
    echo "var jsKeys = ". $jsArray . ";";
    $jsArray = json_encode($Lname);
    echo "var jsNames = ". $jsArray . ";";
    ?>

      for (var i=0;i<jsKeys.length;i++)
      {
      var result = selbox2.options[selbox2.options.length] = new Option(jsNames[i],jsKeys[i]);

        if(i < jsKeys.length-1)
        {
          var result = setTimeout(setOptions(jsKeys[0],document.menu.list3,' '),5);
        }
      }
    return(result);
    }
  </script>

You can do this with simple AJAX or use the jQuery AJAX shorthand methods ($.ajax, $.get, $.post, etc...) depending on exactly what you need to send and if you will be doing something will a result...

I need to send a js var and retrieve it on the same page in php var, which will be used for a php array index.

$list1 = chosen;  //where i need to pass the variable

foreach($list[$list1] as $key => $value)

I have tried this code but the alert shows the entire page contents.

$.post("my.php", {select: chosen}, function(test) {
  alert(test);
});

<?php
$list1 = $_POST['select']; 
echo $list1;
?>

but the alert shows the entire page contents.

It is going to show whatever the output is from my.php. If you want to use the same page, you'll have to detect this event somehow and only show the results you require.

I know how to do this in asp.net, sorry...i'm not a PHP developer. Someone else will most likely guide you accordingly.

Thanks I managed to pass the variable like this, I have another problem though.

   $list1 = $_POST['choice'];

    <script>
    $(document).ready(function(){ 
        $("#list1").change(function(){
            var selected = this.value;
        $.post("my.php", { choice: selected }, function(data){ 
              alert(data); 
        });
      });
    });
    </script>

$list1 is getting the correct string and it is working in the array but the list2 and list3 boxes aren't changing. If I change the code to

$list1 = 'myarrayindex';

everything works fine. Any ideas why it is not working without me putting the string in myself?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.