tags:

views:

99

answers:

3

I have the below jquery function

$(function(){
  $("select#rooms").change(function(){
    $.getJSON("/admin/select.php",{id: $(this).val(), ajax: 'true'}, 
    function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' 
        + j[i].optionDisplay + '</option>';
      }
      $("select#ctlPerson").html(options);
    })
  })
})

as you see I am passing two values to select.php. The second value is a hardcoded text. Instead of that I would like to send some dynamic value. Can I put a php variable there somehow and pass that?

Basically I am calling a backend script on drop down change and passing its id ..but in some cases I will want to pass another value which is also dependent on drop down change.

A: 

Do you mean something like this:

$(function(){
  var isAjax = '<?=isAjax?>';
  $("select#rooms").change(function(){
    $.getJSON("/admin/select.php",{id: $(this).val(), ajax: isAjax}, 
    function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' 
        + j[i].optionDisplay + '</option>';
      }
      $("select#ctlPerson").html(options);
    })
  })
})
karim79
+1  A: 

You can't directly include something that PHP will immediately understand as a variable, but you could use JSON to serialize your javascript variable and then parse it in PHP, turning it back into a variable PHP knows how to deal with.

This works well for complex data like arrays or objects... it's sorta overkill for simple values, which you can just pick up from $_GET/$_POST in PHP.

Gabriel Hurley
A: 

you can pass as the second parameter value the results of a jquery select, just like the first argument

formulate a select that grabs the element you require. it may not be $(this).val() item but perhaps $('isAjax').val()