Jump to content

Recommended Posts

I have a php file that has 2 drop down boxes. Each box is filled with values using a php and a database. I want to make it so that when the user selects a value out of the top drop down box, it automatically updates the values that are in the lower one, without hitting a submit button. I have it working with 2 submit buttons but I dont want users to have to keep hitting submit.

 

Here is the code:

 

<FORM action="dropdown.php" method="get">
<SELECT NAME="cityLargeList">
<?php
//Make a connection to the server... connect("servername","username","password")
$link=mysql_connect("localhost","root") or die("No server connection".mysql_error());
//connect to our database
$db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error());
//construct a SQL query that shows all hotels in the city
$query="SELECT * FROM cityLarge WHERE state_ID = 39";
//run the query
$result=mysql_query($query);	

//for each row returned by our query, do what is in curly braces
while($row = mysql_fetch_array($result)){

//store into a variable the ID # for the current row 
$cityLargeID=$row['cityLarge_ID'];
//store into a variable the Name for the current row
$cityLargeName=$row['cityLarge_name'];
  
//Print out each "<OPTION> with the proper values
print "<OPTION VALUE='$cityLargeID'>$cityLargeName";
}
//close the connection
mysql_close($link);

?>
</SELECT>
<input type="submit" value="Find City" />
</FORM>

<FORM action="test.php" method="get">
<SELECT NAME="citySmallList">
<?php
//Get the value from City Large
$userCityLarge = $_GET['cityLargeList'];
//Make a connection to the server... connect("servername","username","password")
$link=mysql_connect("localhost","root") or die("No server connection".mysql_error());
//connect to our database
$db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error());
//construct a SQL query that shows all hotels in the city
$query="SELECT * FROM citySmall WHERE cityLarge_ID = $userCityLarge";
//run the query
$result=mysql_query($query);	

//for each row returned by our query, do what is in curly braces
while($row = mysql_fetch_array($result)){

//store into a variable the ID # for the current row 
$citySmallID=$row['citySmall_ID'];
//store into a variable the Name for the current row
$citySmallName=$row['citySmall_name'];
  
//Print out each "<OPTION> with the proper values
print "<OPTION VALUE='$citySmallID'>$citySmallName";
}
//close the connection
mysql_close($link);

?>
</SELECT>
<input type="submit" value="Find hotels" />
</FORM>

 

 

That code has 2 submit buttons.  Any ideas?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

 

To find the restaurants in your area, please select where you are staying.

<br />

 

<FORM action="dropdown.php" method="get">

<SELECT NAME="cityLargeList">

<OPTION VALUE='1'>Pittsburgh<OPTION VALUE='2'>Philadelphia<OPTION VALUE='3'>Scranton<OPTION VALUE='4'>Harrisburg<OPTION VALUE='5'>Erie<OPTION VALUE='6'>State College</SELECT>

 

<input type="submit" value="Find City" />

</FORM>

 

<FORM action="test.php" method="get">

<SELECT NAME="citySmallList">

<br />

<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>C:\xampp\htdocs\Click-N-Dine\dropdown.php</b> on line <b>59</b><br />

</SELECT>

<input type="submit" value="Find hotels" />

</FORM>

 

</body>

</html>

 

 

 

Ok, i'll look on line for some javascript.

Not a php issue.

 

Look into the onChange event in HTML/Javascript.

 

You will have to use an ajax call to make it purely dynamic. I would look into jQuery for the Ajax call. The other option is when onchange is activated (meaning they selected a city in the list) it automatically submits the form and re-generates the page. I think that is what you are after.

 

onChange myfriend =)

 

<FORM action="dropdown.php" name="dropdown" method="get">
<select name=myselect onChange="this.dropdown.submit()">

 

Should submit the form onchange.

ok, thats exactly what i need. I added the Javascript there but nothing happened. Sorry, I have no idea about java so i must be missing something.

 

<FORM action="dropdown.php" name="dropdown" method="get">
<SELECT NAME="cityLargeList" onChange="this.dropdown.submit()">

ok, thats exactly what i need. I added the Javascript there but nothing happened. Sorry, I have no idea about java so i must be missing something.

 

<FORM action="dropdown.php" name="dropdown" method="get">
<SELECT NAME="cityLargeList" onChange="this.dropdown.submit()">

 

this "this.dropdown.submit()" it's a bad call

 

Note: great to know its working now!

One other problem i keep getting tho..

 

Since my values are set using the drop down from above, my default values are not being set on the lower drop downs until the top is selected so i keep getting errors like this in the lower ones until I select the top drop menu:

 

Please Select a City Warning: mysql_fetch_array():supplied arguent is not a a valid mySQL result resource:...

 

I tried adding an if statement and moving the default value down that says

if($stateID == NULL) {
   print"<OPTION>Please Select a state";
} else {
   print "<OPTION VALUE='$cityLarge_ID'>$cityLarge_name";
}

 

But that does nothing.

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.