zang8027 Posted December 30, 2008 Share Posted December 30, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/ Share on other sites More sharing options...
ILMV Posted December 30, 2008 Share Posted December 30, 2008 Could you send us the generated HTML code. ILMV Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726217 Share on other sites More sharing options...
zang8027 Posted December 30, 2008 Author Share Posted December 30, 2008 What code? Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726242 Share on other sites More sharing options...
dMilesFox Posted December 30, 2008 Share Posted December 30, 2008 What code? The one you usually get by right clicking with your mouse on the page ("View Source") That way u can get the rendered html source and not the php server source. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726244 Share on other sites More sharing options...
revraz Posted December 30, 2008 Share Posted December 30, 2008 You'll need to use javascript if you want it to auto update. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726246 Share on other sites More sharing options...
zang8027 Posted December 30, 2008 Author Share Posted December 30, 2008 <!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. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726250 Share on other sites More sharing options...
premiso Posted December 30, 2008 Share Posted December 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726255 Share on other sites More sharing options...
dMilesFox Posted December 30, 2008 Share Posted December 30, 2008 Use the "onchange" attribute to call the proper function, Then you can evaluate the selected value on the first drop down box and depending on its value, something needs to be set on the second drop down box. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726258 Share on other sites More sharing options...
zang8027 Posted December 30, 2008 Author Share Posted December 30, 2008 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()"> Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726261 Share on other sites More sharing options...
zang8027 Posted December 30, 2008 Author Share Posted December 30, 2008 Here i attached the 3 files. The file i am working on is dropdown.php. There is also the database and another test file. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726263 Share on other sites More sharing options...
dennismonsewicz Posted December 30, 2008 Share Posted December 30, 2008 change this.dropdown.submit() to this.form.submit() Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726264 Share on other sites More sharing options...
zang8027 Posted December 30, 2008 Author Share Posted December 30, 2008 Hey that works! Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726267 Share on other sites More sharing options...
dMilesFox Posted December 30, 2008 Share Posted December 30, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726270 Share on other sites More sharing options...
premiso Posted December 30, 2008 Share Posted December 30, 2008 change this.dropdown.submit() to this.form.submit() lol, well at least this is the php section and not javascript or else I might be embarrassed. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726272 Share on other sites More sharing options...
dennismonsewicz Posted December 30, 2008 Share Posted December 30, 2008 hey it works... Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726275 Share on other sites More sharing options...
zang8027 Posted December 30, 2008 Author Share Posted December 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138882-solved-form-update-without-submit-button/#findComment-726318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.