0

I can't seem to get the value for a 'selected item' to pass to a JavaScript function with PHP that uses the value to query data from MySQL database -> populates a different drop down list.

Here is my 'HTML' code

<select name="numbers" id="numbers" onChange="populateOtherSelect()">
   <option value="one">one</option>
   <option value="two">two</option>
   <option value="three">three</option>
   <option value="four">four</option>
</select>

<?php echo $option ?>

Here is my 'JavaScript' function code

function populateOtherSelect() {
 <?php
   // code that opens db connection

   $numbers = $_POST['numbers']; 

   $query = "select*from Database.Table where Something = 'Something' and Numbers like '%".$numbers."%'";

   $result = mysql_query($query);

   $option = "";

   while($row = mysql_fetch_array($result))
    {
        $OtherOptions = $row["OtherOptions"];
        $option.="<option value=\"$OtherOptions\">".$OtherOptions."</option>\r\n";
    }

   // code that closes db connection
?>

}

Any information will be helpful.

Thanks very much

4
  • So what's the problem? Is the $numbers empty, or do you have a problem with the query? Jun 28, 2011 at 22:25
  • If you want to do it like you have now, you have to post the form, which will cause the site to reload every time you change the list. Otherwise, it's AJAX.. have a look at the answer from @jeroen. Jun 28, 2011 at 22:36
  • Yes I have looked at his answer. I am looking into AJAX now. I have not really looked at AJAX. I am new to programming. Thanks
    – TheRealDK
    Jun 28, 2011 at 22:44
  • To spell it out explicitly (since none of the other comments or answers seem to have done so yet): PHP code is run on the web-server before the page is downloaded to the user's web browser. JavaScript code is run in the web browser. This means that your JavaScript functions can't call your PHP functions and vice versa. As others have suggested, when your select element is changed you have to send another request to the web-server and either reload the whole page with your second select populated appropriately, or (the preferred way) go with AJAX to get the new data without reloading the page.
    – nnnnnn
    Jun 28, 2011 at 23:39

3 Answers 3

2

It seems your javascript code is part of the html / php document and that´s not going to work; the php is run only at initial page-load and at that moment there probably is no $_POST variable.

What you need to do, is make an ajax call to a php script at the moment your first select is changed and use the response from that ajax call to populate your second select.

So basically your javascript function would have to look like:

function populateOtherSelect() {
  /*
   * 1. get the value of the numbers select
   * 2. make an ajax call to a php script / the server that gets some information from a database
   * 3. use the response from the ajax call to populate your second select
   */
}
1
  • +1 Just realized he had the onChange calling a php function. Jun 28, 2011 at 22:32
0

I think you need to add a space in your select statement:

$query = "select * from Database.Table where Something = 'Something' and Numbers like '%".$Numbers."%'";
0

First of all, the $Numbers variable doesn't exist, it's $numbers.

1
  • Well I change the code so it is easier to follow all the naming has the same case.
    – TheRealDK
    Jun 28, 2011 at 22:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.