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
Share on other sites

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.

Link to comment
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
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
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.