开发者

javascript and PHP variable passing

开发者 https://www.devze.com 2023-04-03 19:32 出处:网络
I have two select box and the second select box value is dependent on the first select box. So basically what I am trying to do is that after a user makes a selection on the first select box, it will

I have two select box and the second select box value is dependent on the first select box. So basically what I am trying to do is that after a user makes a selection on the first select box, it will then store this value of the first select box as a variable. Then it will query my database to populate the second select box, based on the first selected value. Question is, how do I pass in the var I have in the first select box to PHP? I've read other post and people said it's impossible, but if 开发者_StackOverflow中文版it is then how do people do this? AJAX?


If I understand correctly, then yes, using AJAX is really your only choice.


Indeed, you can, with AJAX, call something like 'getSelectionData.php?data=' + select1.value, which returns a JSON array of data based on the selection in the first select box. You then parse the data and enter it into the second select box.


Add a onchange event listener to the first select box:

document.getElementById("select1").addEventListener("change", function(ev){
   var yourstoredvariable = this.value;
   someFunctionThatCallsAjax(yourstoredvariable);
}, true);


I assume that you have a Country/City dropdowns, You can do it in two ways, the good AJAX way and the bad way (refresh page on change), I'm gonna describe the good way in jQuery, or at least what I see it good.

this is the client-side (i.e. client) code:

<select id="country">
  <option value="1">Canada</option>
  <option value="2">UK</option>
</select>
<select id="city" disabled="disabled"></select>
<script type="text/javascript">
  $('#country').change(function(){
      $('#city').load('/ajax-city.php', {id: $(this).val()});
  });
</script>

This is the ajax-city.php code (server):

<?php
$countryId = $_REQUEST['id'];
$cities = array(); // Get it from db, using mysql_query or anything you want to use
foreach($cities as $city) {
  echo "<option id=\"{$city['id']}\">{$city['name']}</option>";
}

PS. you would need to include jQuery in your html code, and of course put the two files in the same directory (or change the $.load path).

This particular code is not tested, I've just written it. But it usually works fine to me this way.


You will have an onchange event on the first <select> that will query the server using Ajax with the value of the selected <option> that will return the <option> elements with which to populate the 2nd <select> element.


Question is, how do I pass in the var I have in the first select box to PHP?

I see no problem here.
Just usual, an ordinary html form using GET method.
What's so big about it?


If I see correct you're using Jquery. So you can do this like this:

$('#idOfSelectBox1').change(function(){
jQuery.ajax({
    type: "GET",
    url:"yourdomain.com/script.php",
    data:"selectBox:'"+$('#idOfSelectBox1').val()+"'",
    success:function(result){
        //do smth with the returned data
    }
});
});

in the script.php do your magic and echo what you want to pass back to js

0

精彩评论

暂无评论...
验证码 换一张
取 消