Page 1 of 1
Need help with sessions
Posted: Tue Mar 11, 2008 4:24 pm
by henry8
I need to have the variables that are typed into a form, name, email, phone etc. still be in the form when the person returns to it from another page. I have been trying SESSIONS.
Code: Select all
<?php
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['phone'] = $_POST['phone'];
$_SESSION['email'] = $_POST['email'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
}
?>
and this in the form.
<input type="text" name="name" value="<?=$name = $_POST['name'];?>" size="30" maxlength="40" />
This makes it stay in the fields of the form after you click submit but it is not there when you return to the page. I have <?php session_start(); ?> on each page that the person would go to.
Can anyone help please.
Re: Need help with sessions
Posted: Tue Mar 11, 2008 4:38 pm
by Christopher
You are overwriting the session variables each time. Try something like:
Code: Select all
if (isset($_SESSION['name'])) {
$_SESSION['name'] = $_POST['name'];
}
Re: Need help with sessions
Posted: Tue Mar 11, 2008 5:19 pm
by henry8
Hi Christopher, thank-you for your promt reply. I tried that though but there is still nothing there when you return to the page.
What about the code I'm using in the fields themselves.
Code: Select all
<input type="text" name="name" value="<?=$name = $_POST['name'];?>" size="30" maxlength="40" />
Peter
Re: Need help with sessions
Posted: Tue Mar 11, 2008 6:30 pm
by Peter Anselmo
Two things...
I think arborint made a typo. This makes more sense to me:
Code: Select all
if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
}
That way, it will only overwrite the value when the form has just been submitted.
Also, you'll want to change your input line to read:
Code: Select all
<input type="text" name="name" value="<?=$name = $_SESSION['name'];?>" size="30" maxlength="40" />
After all, you're taking care to store the variable in the _SESSION scope, you should be checking there to prefill the form value, as the _POST scope will not persist between multiple requests.
Re: Need help with sessions
Posted: Tue Mar 11, 2008 6:52 pm
by Christopher
Thankfully Peter knew what I meant!

Excellent.
Re: Need help with sessions
Posted: Wed Mar 12, 2008 7:27 am
by henry8
Hi there, thank-you again for your help, but it still doesnt work. Would it have anything to do with fact that there is a lot of other code on the page as the info is going into a database and some of it returned to the same page. What I'm trying to do is set up an auction site for the NGO I volunteer for. I've got the first part working. You enter your name, phone, email, bid and name if you want it to appear. Click 'Make your Bid' and the amount of the bid and name show up on your screen immediately. Then you click back to the page where the auction items are (the new info is there too of course), 21small paintings. All that works fine. But if you want to raise your bid or even bid on another painting you have to fill out all of your details again. I really do want this to work, it's very frustrating. Peter
Re: Need help with sessions
Posted: Wed Mar 12, 2008 7:27 am
by henry8
Hi there, thank-you again for your help, but it still doesnt work. Would it have anything to do with fact that there is a lot of other code on the page as the info is going into a database and some of it returned to the same page. What I'm trying to do is set up an auction site for the NGO I volunteer for. I've got the first part working. You enter your name, phone, email, bid and name if you want it to appear. Click 'Make your Bid' and the amount of the bid and name show up on your screen immediately. Then you click back to the page where the auction items are (the new info is there too of course), 21small paintings. All that works fine. But if you want to raise your bid or even bid on another painting you have to fill out all of your details again. I really do want this to work, it's very frustrating. Peter
Re: Need help with sessions
Posted: Wed Mar 12, 2008 10:31 am
by Zoxive
Code: Select all
<?php
session_start();
function form_value($name){
if(!empty($_POST[$name])) // Is there post?
$_SESSION[$name] = $_POST[$name]; // Set Session
if(!empty($_SESSION[$name])) // IF there is session
return $_SESSION[$name]; // return the value
return NULL; // No session? no value
}
$name = form_value('name');
$phone = form_value('phone');
$email = form_value('email');
Then change your form values to..
Code: Select all
<input type="text" name="name" value="<?php echo $name; ?>" size="30" maxlength="40" />
You don't need to set them again with = $_POST['name'].
Re: Need help with sessions
Posted: Wed Mar 12, 2008 4:38 pm
by henry8
Hi, Thank-you very much, I do appreciate all your help. It works beautifully. Just one little hang up though. When you have filled out the form and clicked 'send' the bid and the name changes as it should but the name moves into and overwrites the the email address?? Any idea why that happens? I've checked and rechecked that all the code is the same for all fields etc. but can find nothing wrong there. Peter
Re: Need help with sessions
Posted: Wed Mar 12, 2008 5:40 pm
by Zoxive
henry8 wrote:Hi, Thank-you very much, I do appreciate all your help. It works beautifully. Just one little hang up though. When you have filled out the form and clicked 'send' the bid and the name changes as it should but the name moves into and overwrites the the email address?? Any idea why that happens? I've checked and rechecked that all the code is the same for all fields etc. but can find nothing wrong there. Peter
If you can show us
ALL of the relative code we should be able to help.
Re: Need help with sessions
Posted: Wed Mar 12, 2008 7:31 pm
by henry8
OK here is the whole page.
Code: Select all
<?php
session_start();
function form_value($name){
if(!empty($_POST[$name])) // Is there post?
$_SESSION[$name] = $_POST[$name]; // Set Session
if(!empty($_SESSION[$name])) // IF there is session
return $_SESSION[$name]; // return the value
return NULL; // No session? no value
}
$name = form_value('name');
$phone = form_value('phone');
$email = form_value('email');
$email = form_value('bid');
$email = form_value('bidname');
?>
<?php require_once('../../Connections/auction.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO num_1 (name, phone, email, bidname, bid) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['phone'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['bidname'], "text"),
GetSQLValueString($_POST['bid'], "int"));
mysql_select_db($database_auction, $auction);
$Result1 = mysql_query($insertSQL, $auction) or die(mysql_error());
}
mysql_select_db($database_auction, $auction);
$query_pic_1 = "SELECT num_1.bid, num_1.bidname FROM num_1 ORDER BY num_1.bid DESC";
$pic_1 = mysql_query($query_pic_1, $auction) or die(mysql_error());
$row_pic_1 = mysql_fetch_assoc($pic_1);
$totalRows_pic_1 = mysql_num_rows($pic_1);
?>
<!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=iso-8859-1" />
<title>ICAI Online Auction</title>
<link href="../auction.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
</head>
<body>
<table align="center" width="750" bgcolor="#ff9900">
<tr><td>
<table width="750" bgcolor="#fdfcc1">
<tr><td width="1119">
<table width="750" align="center" cellpadding="0">
<tr>
<td width="750"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="750" height="125">
<param name="movie" value="../flash/title2.swf" />
<param name="quality" value="high" />
<embed src="../flash/title2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="750" height="125"></embed>
</object></td>
</tr>
<td><br /></td>
</tr>
<tr>
<td bgcolor="#fdfcc1"><table width="720" align="center" cellpadding="0">
<tr>
<td valign="top" width="20"> </td>
<td valign="top" width="300">
<img src="../images/chalkhorse1.jpg" width="250" height="166" /></td>
<td valign="top" width="60">
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<table width="474">
<tr valign="baseline">
<td width="16" align="right"> </td>
<td colspan="2" align="right"><div align="left"><span class="fsize2">Make your Bid for</span><span class="style1">: </span></div></td>
<td width="58"></td>
<td width="219"><div align="center"><span class="style1"><a href="../index.php" class="fsize2">Back</a></span></div></td>
<td width="3"></td>
</tr>
<tr valign="baseline">
<td align="right"></td>
<td colspan="5" align="right"><div align="left"><span class="titles1">No. 1 Chalk Horse </span></div></td>
</tr>
<tr valign="baseline">
<td align="right"></td>
<td colspan="3" align="right"><div align="left" class="style1"><span class="fsize2">Highest Bid: </span> <span class="titles4">$<?php echo $row_pic_1['bid']; ?></span></div></td>
<td align="right"><div align="left" class="fsize2"><?php echo $row_pic_1['bidname']; ?></div></td>
<td align="right"> </td>
</tr>
<tr valign="baseline">
<td></td>
<td colspan="5" align="right"><div align="center"></div></td>
</tr>
<tr valign="baseline">
<td></td>
<td colspan="5" align="right"> </td>
</tr>
<tr valign="baseline">
<td></td>
<td colspan="5"></td>
</tr>
<tr valign="baseline">
<td></td>
<td width="147"><div align="right"><span class="titles3">Name </span></div></td>
<td colspan="4"><input name="name" type="text" value="<?php echo $name; ?>" size="30" maxlength="40" /></td>
</tr>
<tr valign="baseline">
<td></td>
<td align="right"><span class="titles3">Phone </span></td>
<td colspan="4"><input name="phone" type="text" value="<?php echo $phone; ?>" size="20" maxlength="20" /></td>
</tr>
<tr valign="baseline">
<td></td>
<td align="right"><span class="titles3">Email </span></td>
<td colspan="4"><input name="email" type="text" value="<?php echo $email; ?>" size="30" maxlength="60" /></td>
</tr>
<tr valign="baseline">
<td></td>
<td></td>
<td colspan="4"></td>
</tr><br />
<tr valign="baseline">
<td></td>
<td align="right"><span class="titles1">Make a Bid </span></td>
<td colspan="4"><input name="bid" type="text" value="" size="20" maxlength="8" /></td>
</tr>
<tr valign="baseline">
<td></td>
<td colspan="5" align="right"><div align="center" class="navbar">If you wish to have your name displayed with your bid <br />
please re-type it below</div></td>
</tr>
<tr valign="baseline">
<td></td>
<td></td>
<td colspan="4"></td>
</tr>
<tr valign="baseline">
<td></td>
<td align="right"> </td>
<td colspan="4"><input name="bidname" type="text" value="<?php echo $bidname; ?>" size="30" maxlength="40" /></td>
</tr>
<tr valign="baseline">
<td></td>
<td align="right"><div align="left" class="titles5"><a href="../index.php">Back</a></div></td>
<td colspan="4"><input name="submit" type="submit" value="Send Bid " /></td>
</tr>
<tr valign="baseline">
<td></td>
<td align="right"> </td>
<td colspan="4"> </td>
</tr>
<tr valign="baseline">
<td></td>
<td align="right"> </td>
<td colspan="4"> </td>
</tr>
<tr valign="baseline">
<td></td>
<td></td>
<td colspan="4"></td>
</tr>
<tr valign="baseline">
<td></td>
<td></td>
<td colspan="4"></td>
</tr>
<tr valign="baseline">
<td></td>
<td colspan="5" align="right" nowrap="nowrap"><div align="center"><span class="fsize1">The Institute of Cultural Affairs International never shares <br />
your contact information or giving history with any external parties</span>.</div></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form2" />
</form></td>
</tr>
<tr>
<td valign="top"></td>
<td colspan="2" valign="top"></td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr></table>
</td></tr></table>
</body>
</html>
<?php
mysql_free_result($pic_1);
?>
Re: Need help with sessions
Posted: Thu Mar 13, 2008 8:01 am
by Zoxive
henry8 wrote:
Code: Select all
<?php
// ....
$name = form_value('name');
$phone = form_value('phone');
$email = form_value('email');
$email = form_value('bid');
$email = form_value('bidname');
// ....
You are setting the variable $email for the form value of email, bid and bidname..
->
Code: Select all
<?php
// ....
$name = form_value('name');
$phone = form_value('phone');
$email = form_value('email');
$bid = form_value('bid');
$bidname = form_value('bidname');
// ....
Re: Need help with sessions
Posted: Thu Mar 13, 2008 9:03 am
by henry8
Thank-you so very much. I can really be dumb sometimes. Thanks to you this project will get off to a good start. Peter:)