mikefrederick Posted October 5, 2007 Share Posted October 5, 2007 I have a form that has two dropdown boxes. The options of each dropdown box are read from a different field in a table of a database. The first box is for Regions, the second for Countries. Using sessions, I would like to make it so that when you select the first box, the second one is updated to the countries listed in the region that was selected. I can make it so that the first box lists all regions and the second lists all countries, but I only want the second to list countries that are in the selected region. here is my code, pay attention to the bold part as I need to change $_SESSION['region']='North America' to $_SESSION['region']='whatevertheuserjustselected' - is there anyway to do this? <form name="form1" method="post" enctype="multipart/form-data" action="addstate.php"> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td class="text2">Select Region</td> <td> <? mysql_select_db($database_localhost, $localhost); $query_Rs = "SELECT * FROM countries"; $Rs = mysql_query($query_Rs, $localhost) or die(mysql_error()); $totalRows_Rs = mysql_num_rows($Rs); ?> <select name="region" class="text2" id="region"> <? while ($row_Rs = mysql_fetch_assoc($Rs)) { ?> <option value="<? echo $row_Rs['region']; ?>"><? echo $row_Rs['region']; $_SESSION['region']='North America' ?></option> <? } ?> </select> <? $region=$_SESSION['region']; $query_Rs = "SELECT * FROM countries where region='$region'"; $Rs = mysql_query($query_Rs, $localhost) or die(mysql_error()); $totalRows_Rs = mysql_num_rows($Rs); ?> <select name="region2" class="text2" id="region2"> <? while ($row_Rs = mysql_fetch_assoc($Rs)) { ?> <option value="<? echo $row_Rs['countryid'];?>"><? echo $row_Rs['countryname'];?></option> <? } ?> </select> </td></tr> <tr> <td class="text2">State Name</td> <td><input name="statename" type="text" class="text2" id="statename"></td> <td><input name="Submit" type="submit" class="text2" value="Add State"></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/ Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 Code tags, please. Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362759 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 to go this route, you'll need to post the form so the code knows what the first selection is. that means adding an onchange() to the first select that causes the form to be submitted, then you'll be able to get what was selected. p.s. and you don't need sessions to do this. Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362760 Share on other sites More sharing options...
mikefrederick Posted October 5, 2007 Author Share Posted October 5, 2007 yeah I know you can do it with javascript I just wanted to see if you could do it by somehow saving there entry in the first field into the session. right now, with $_SESSION['region']='North America' it shows only the countries in North America, so if I could somehow change North America to just be whatever they selected in the option w/ echo $row_Rs['region']; I would be good. So I need to store what they select, hopefully in that session tag, and apply that to the field below. Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362767 Share on other sites More sharing options...
mikefrederick Posted October 5, 2007 Author Share Posted October 5, 2007 or it would help if you could tell me how to modify the code to add onchange() because I am not familiar with javascript. Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362773 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 i'm not a JS guru either, but something close to this should work: <SELECT NAME='region' onchange='this.form.submit();'> that should submit the form, at which time you can get the value of $_POST['region'] and then use that for the SELECT to fill in the second dropdown. no sessions involved. Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362788 Share on other sites More sharing options...
mikefrederick Posted October 5, 2007 Author Share Posted October 5, 2007 thanks a lot bluesky, do you have aim? Quote Link to comment https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362822 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.