-1

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.

2
  • 1
    I don't understand your question Commented Sep 8, 2009 at 3:54
  • Where would the 'dynamic value' you reference come from? You can't pass php variables from outside php, but php and js will have access to the same resources and select.php might be able to get the data on it's own. (Assuming you're not passing client-side information) Commented Sep 8, 2009 at 3:57

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
    })
  })
})

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.