Jump to content

How to make a new drop down box open based off selection in previous box


Styler001

Recommended Posts

Hi, all.  Looking around here, it looks like this is a great place to get some help with the problem I have.  I'm a noob at this PHP stuff, so please take it easy on me.

For a website I'm working on, I am trying to open a second drop down box that will contain the type of information you choose in the first drop down box.  At this point, I'm not really interested in any new web pages being opened.  I'd just like to ask for some help with the general coding of this so I can study it and proceed.

The first drop down box you would see contains the following choices: CITIES and NAMES

So, if you choose CITIES in the first drop down box, the second drop down box should contain a list of cities.  If you choose NAMES in the first box, the second drop down box should contain a list of names.

What I don't understand is how to assign the variables and whatever else may be needed, and from there how to actually display either the list of cities or names in that second box.

I understand that in normal English it would be something like:

[code]If Choice = Cities
  Then open the new drop down box containing the list of cities
Else
  Open the new drop down box containing the list of names[/code]

So, translating that into PHP is where I'm stuck.  And my not-so-impressive beginning for the PHP script is:

[code]<?php
  {
  echo '<form>';
  echo '<select name="Choice">';
  echo '<option value="Cities">Cities</option>';
  echo '<option value="Names">Names</option>';
  echo '</select>';
  echo '</form>';
  }
?>[/code]

I'd appreciate any help you can give this noob.  Thanks.
Link to comment
Share on other sites

I think I'd like to stick with PHP.  Wouldn't using a Java script mean that the receiving computer would have to have Java installed?  And with a java script, I'm even more lost than I am with PHP.  My PHP experience is very basic still, but my Java experience is non-existent.
Link to comment
Share on other sites

<?php
  {
  echo '<form>';
  echo '<select name="Choice">';
  echo '<option value="Cities">Cities</option>';
  echo '<option value="Names">Names</option>';
  echo '</select>';
  echo '</form>';
  }
?>
/*It is assumed that u have choosed the option Names.Now the data will befetched from the Table:"table_names".There are two fields in the table.one is:"name_id" another is:"name".And if Each name has some Sub name theb choost that.*/
<?php
if($Choice)
{
$sql="select `name_id` from `table_names` where `name`='$Choice'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res);
        $t=$row[name_id];
       
$sql1="select * from `subtable_names` where `name_id`='$t'";
    $res1=mysql_query($sql1);
print "<select name=Choice2>";
print "<option selected>Choose One</option>";
while($row1=mysql_fetch_array($res1))
{
  print "<option>$row1[sub_name]</option>";
}
print "</select>";
print "&nbsp;&nbsp;&nbsp;";
print "<INPUT TYPE=\"submit\" value=\"OK\">";
}?>
<?php
if($Choice2)
{
        $sql="select `subname_id` from `subtable_name` where `sub_name`='$Choice2'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res);
        $t=$row[subname_id];
}
?>

Before run this programe make sure urself that u have created the database already.it's sure that the code will be run properly.
Link to comment
Share on other sites

Wow!  That was a little more complicated than I was ready for.  Eventually, I will be using a MySQL database, but for now I'd just like to get this figured out using something like the following code.  I know it's very crude and primitive, and doesn't work like I want it to, but I think I should get an understanding of how to accomplish this before I move on.

If you run this code, it displays the first drop down box like I want it to, but it also displays one of the other drop down boxes that shouldn't be displayed until after you make a choice from the first one.

[code]<html>
<body>

<?php {
  echo '<br><br><br><br>';
  echo '<b>Search By</b>';
  echo '<form>';
  echo '<select name="Choice">';
  echo '<option value="City">City</option>';
  echo '<option value="Name">Name</option>';
  echo '</select>';
  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp';
  echo '<INPUT TYPE="submit" value="Submit">';
  echo '</form>'; }

  if ($Choice == "City") {
    echo '<br><br>';
    echo '<b>Select City</b>';
    echo '<form>';
    echo '<select name="City">';
    echo '<option value="New_York">New York</option>';
    echo '<option value="Los_Angeles">Los Angeles</option>';
    echo '</select>';
    echo '</form>'; }
  else {
    echo '<br><br><br><br><br><br>';
    echo '<b>Select Name</b>';
    echo '<form>';
    echo '<select name="Name">';
    echo '<option value="Joe">Joe</option>';
    echo '<option value="Tom">Tom</option>';
    echo '<option value="Fred">Fred</option>';
    echo '</select>'; }
?>

</body>
</html>[/code]
Link to comment
Share on other sites

If you're sticking with strictly PHP there is no way to know what they've selected in the first box [i]until[/i] they've submitted the form.

Here is what you're looking at:
1) Display first drop down box on a form with a submit button

2) User makes selection and submits form

3) Take the user's selection and redisplay the page but add the new drop down box to it.

PHP lives and runs only on the server, never on the client.  If you want things to update / refresh / occur [i]without[/i] submitting and reloading the page, your solution lies in the domain of Javascript.
Link to comment
Share on other sites

Well, I understand what you're saying.  But, I'd rather not get into the javascript at this time.

Like I said, I'm a noob at PHP, and I know what I'm asking for is probably pretty basic.  But I just can't seem to figure out why that second drop down box is even appearing right away, when it shouldn't be.  Or, why the City drop down box doesn't display if I select City.

When I select City, then click Submit, nothing happens.  If I select Names, then click Submit, I can't tell if anything happens because the Names drop down box is already displayed.

I realize (and appreciate) that you guys are trying to get me from here to a "better" there because you know what this should all look like.  But, I'd like to see what needs to be fixed in the code that I have just so I know what to expect when I get into the more in-depth coding later.

Also, can someone tell me, if I use javascript, does the person viewing my pages need to have Java installed?  If that's the case, I don't want to use javascript because I don't want the viewer to be [u]required[/u] to install anything to view my pages.
Link to comment
Share on other sites

You should specify the [i]method[/i] attribute of your form to be [i]post[/i].

Here's a simple example:
[code]
<html>
<body>
<?php
  $form = '<form name="myform" action="" method="post">'
            . '<select name="mysel">'
            . '<option value="1">1</option>'
            . '<option value="2">2</option>'
            . '<option value="3">3</option>'
            . '</select>'
            . '<input type="submit" name="filter" value="Filter" />';
  // Check if the filter button was pressed, if so we can display more fields
  if(count($_POST) && isset($_POST['filter'])){
    $form .= '<select name="anothersel">'
              . '</select>';
  }
  $form .= '<input type="submit" name="submit" value="Submit" />';
  echo $form;
?>
</body></html>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.