1

I'm currently faced with a dilemma. I'm trying to build a menu that will display an array of data from a database. The user will then select an item from that list, and based on this, I need to make a database call to retrieve that data associated with that. Here is the current code that I have.

function foobar() {
  $first_option_sql = "SELECT * from {location} l ORDER BY name ASC";
  $list_first_option = db_query($first_option_sql);

  $output .= "<p><b>Please select the primary location that you would like to check.</b><p>"
  $output .= "<br><b>Location: </b> <select id='list'><option>Available locations</option>";

  while($first_option_available = db_fetch_object($list_first_option)){
    $output .="<option locationid='$first_option_available->lid'>".$first_option_available->name."</option>";
  }

  $output .="</select><input type=submit value='Search'>";
  $output .="<p><b>Children: </b><select id='child'><option selected='yes'>Available children</option><p>";

  $children_available_sql = "SELECT complexquery WHERE lid='%d' ORDER BY l.name ASC";
  $list_children = db_query($children_available_sql);

  while($children_available = db_fetcj_object($list_children)){
    $output .="<option>".$children_available->name."</option>";
  }

  return $output;

}

The problem here is that using this code, am unable to retrieve the value of the selected "first option", thus the second query never actually taking place; this does not populate the second list.

I was thinking of using AJAX or Javascript, but I'm not too familiar with either. Therefore, I wanted some insight as to how I may be able to perform this.

3
  • If you're using PHP alone, you can't really do this without submitting your form and redisplaying the page, since PHP is processed server side. You could, as you mention, do it much more effectively using Ajax, passing the selected first option to a PHP page that will then return JSON data from a db request containing the values for the second list. Commented Dec 30, 2011 at 16:58
  • Since I'm not too familiar with AJAX, I'm wondering how this can be done. Since I'm basically generating webpages using PHP, I think that it might be a bit better to have this function lead to another. Commented Dec 30, 2011 at 17:06
  • Well, then you submit the form to itself onChange of the first option list, and pass the value of the first select list as a POST or GET param. Then, at the head of your page, check if that param isset. If it is, then you know the form was submitted, and you use that value to trigger the second select list lookup. Commented Dec 30, 2011 at 20:34

1 Answer 1

1

First off, your HTML is invalid or simply missing details.

You need a name element for your select box:

<select name="lid" id='list'>

locationid won't work when submitting a form:

<option locationid='$first_option_available->lid'>

You need a value attribute instead:

<option value='$first_option_available->lid'>

With the proper HTML, $_POST['lid'] will be the location id that you want. You can pass this to your function:

$menu = foobar($_POST['lid']);

function foobar($lid) {
    //...
}
Sign up to request clarification or add additional context in comments.

7 Comments

As for the whole POST['lid']... is this simply because the POST command only passes values within an HTML tag and not custom values (i.e. locationid)?
Correct. The value will only end up in POST if the right HTML attribute exists.
If you use javascript you could use whichever attribute you want to get the value from, but I see no reason to use anything other than the default html name and value attributes, even with javascript.
This is a really nice solution, but the only problem that I have is that I need to create this within the same function. Meaning, everything needs to be nested within the function foobar(). What I'm thinking is doing something a bit differently -- namely get the variable <option VALUE = [this is what I care about]> and then pass that onto another page.
I don't understand what you want and how this doesn't work. You can do everything in the one function if you really want to. You can take that option value and do whatever you want with it.
|

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.