Jump to content

HELP!! SESSIONS!!


mikefrederick

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/72007-help-sessions/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362767
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/72007-help-sessions/#findComment-362788
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.