Jump to content

Session using Drop Down list


aeafisme23

Recommended Posts

I guess I am just confusing myself more than anything right now as i run in circles.

First Page: Session1.php
[code]<?php
session_start();
session_register("state", "city");
ob_start();
include("header.php");
?>

<table width="798" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
<p>Your Choice was:
<?php
echo $state;
echo ", ";
echo $city;
?>
<br><br>
<a href="session1.php">Page 1</a> | <a href="session2.php">Destroy Session But Still Show on Page 2 for last time!</a></p>
<p>You are at Session page 2.</p>
</td>
</tr>
</table>
</body>
</html>[/code]


I put the logic behind the include file for the header, as i want to use the header as the session through out the whole site, kinda like a customizeable header for every page of a site: header.php
[code]<SCRIPT language=JavaScript src="dropdown.js" type=text/javascript></SCRIPT>

<SCRIPT language=JavaScript type=text/javascript>
var states = new Array(
new Array("dropdown","state","city"),

new Array(true,"IN|Indiana","Evansville"),
new Array(true,"IN|Indiana","Fort Wayne"),
new Array(true,"IN|Indiana","Indianapolis"),
new Array(true,"IN|Indiana","Kokomo"),
new Array(true,"IN|Indiana","Lafayette"),

new Array(true,"KY|Kentucky","Bowling Green"),
new Array(true,"KY|Kentucky","Charleston-Huntington"),
new Array(true,"KY|Kentucky","Lexington"),
new Array(true,"KY|Kentucky","Louisville"),

new Array(true,"WY|Wyoming","Casper-Riverton"),
new Array(true,"WY|Wyoming","Cheyenne-Scottsbluff")
);
</SCRIPT>


<body onload="dropdown(1,states);">
<?php
echo "<form name=\"dropdown\" action='".$_SERVER['php_self']."' method=\"post\">";
?>
<table width="798" border="1" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="3" width="400">
<?php
if ($state == "IN" && $city =="Kokomo")
{
echo"KOKOMO, INDIANA<br><img src=\"x.jpg\" width=\"20\" height=\"20\">";
}

else
{
echo "A different Selection that isn't IN and Kokomo<br><img src=\"x.jpg\" width=\"20\" height=\"20\">";
}
?>
</td>
</tr>
<tr>
<td valign="top"><p>State:</p></td>
<td><select style="width:180px;" onchange="update(this,states)" size="1" name="state">
<option selected name="formstate"><b>Select State</b></option></select><br><br>
</td>
</tr>
<tr>
<td><p>City:</p></td>
<td><select onchange="update(this,city);" size="1" name="city">
<option selected><b>Select City</b></option></select></td>
</tr>
<tr>
<td colspan="2"><center><p>Select a city that best represents where you live</p></center></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>[/code]



Second Page: Session2.php (used to destroy session along with unregistering) Note: act like there are 100's of pages in between here, i just have this so i can try to destroy the session.
[code]<?php
session_start();
session_unset();
session_destroy();
ob_start();
include("header.php");
?>

<table width="798" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
<p>Your Choice was:
<?php
echo $state;
echo ", ";
echo $city;
?>
<br><br>
<a href="session1.php">Page 1</a> | <a href="session2.php">Destroy Session But Still Show on Page 2 for last time!</a></p>
<p>You are at Session page 2.</p>
</td>
</tr>
</table>
</body>
</html>
[/code]



I guess heres my logic.....

Declare
Session Start in all pages
Register variables.... ( do i register only in header or every page)
When closing session am i doing it right.

This is driving me insane, if you dont want to write code to help me out thats fine, but i just need help with logic as well, becuase i wont learn with out knowing, much appreicated to everyone who looks.
Randy
Link to comment
https://forums.phpfreaks.com/topic/33707-session-using-drop-down-list/
Share on other sites

the way  you are doing session vars is depreciated. instead, do it this way:

page1.php
[code]
<?php
  session_start();
  $_SESSION['blah'] = 'something';
  header('Location: page2.php'); exit();
?>
[/code]

page2.php
[code]
<?php
  session_start();
  if ($_SESSION['blah']) {
      echo $_SESSION['blah'];
  } // end if session var exists
[/code]

you do have this part right tho, as far as killing a session:

[code]
session_unset();
session_destroy();
[/code]
If header is included on every page, then set the session there, as well as all variables you want available throughout the site.

As a side note, I find it easier to drop in and out of php instead of fighting with escaping quotes e.g.

Try changing this...
[code]
<body onload="dropdown(1,states);">
<?php
echo "<form name=\"dropdown\" action='".$_SERVER['php_self']."' method=\"post\">";
?>
<table width="798" border="1" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="3" width="400">
<?php
if ($state == "IN" && $city =="Kokomo")
{
echo"KOKOMO, INDIANA<br><img src=\"x.jpg\" width=\"20\" height=\"20\">";
}

else
{
echo "A different Selection that isn't IN and Kokomo<br><img src=\"x.jpg\" width=\"20\" height=\"20\">";
}
?>
</td>
[/code]

To This.

[code]
<body onload="dropdown(1,states);">
<form name="dropdown" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<table width="798" border="1" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="3" width="400">
<?php
if ($state == "IN" && $city =="Kokomo")
{
?><!-- drop out of php and back into straight html  -->
          KOKOMO, INDIANA<br><img src="x.jpg" width="20" height="20">
<?php // jump back into php
else
{
?> <!-- drop out of php and back into straight html  -->
A different Selection that isn't IN and Kokomo<br><img src="x.jpg" width="20" height="20">
<?php //jump back into php
}
    ?>
</td>
[/code]

and this
[code]<td>
<p>Your Choice was:
<?php
echo $state;
echo ", ";
echo $city;
?>
<br><br>
[/code]

to this

[code]<td>
                <p>Your Choice was:
<?php echo $state; ?>,<?php echo $city; ?>
<br><br>
[/code]

I simply find it easier to drop in and out when needed instead of dealing with escaping quotes.

Nate

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.