Page 1 of 2
setting $_SESSION variables
Posted: Fri Mar 10, 2006 3:59 am
by 5leander5
I am trying to start a session and to set each individual $_POST variable if it has been entered by the user of the form.
the first few lines of my php script code is as follows:
<?php
session_start() ;
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
Will this achieve it's aim? It does not appear to be working at the moment.
I then check each variable from the within the form using:
<input name="Name" type="text" value="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" size="29">
Will this check the variable correctly?
Posted: Fri Mar 10, 2006 4:34 am
by s.dot
It looks good to me. Are you getting any error messages?
Posted: Fri Mar 10, 2006 4:47 am
by 5leander5
2 things. My code uses the value attribute rather than the id tag, as follows:
Code: Select all
<input name="Name" type="text" [b]value[/b]="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" tabindex="2" size="29">
I am not gettiing an error message. however, if I enter my name in the name field in the form, that field is blank after pressing the submit button.
Posted: Fri Mar 10, 2006 5:24 am
by newmember
hey
I then check each variable from the within the form using:
<input name="Name" type="text" value="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" size="29">
why don't you
var_dump($_POST) and check that what you get is exepcted
then
var_dump($_SESSION) after
session_start() and see what you got there...
Posted: Fri Mar 10, 2006 5:35 am
by 5leander5
I do the following after starting the session:
Code: Select all
if(isset($_POST['submit'])){
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
}
var_dump($_SESSION['Name']);
which returns NULL. Can you see why this is returning a NULL?
Posted: Fri Mar 10, 2006 5:41 am
by newmember
just do var_dump($_SESSION)
when you write:
var_dump($_SESSION['Name']);
you assume that there is an entry in $_SESSION with key 'Name'...
assumptions don't help

Posted: Fri Mar 10, 2006 5:44 am
by 5leander5
When I do a var_dump on $_SESSION, I get
array(0) { }
Therefore, $_SESSION has not been set for some reason. Any ideas why?
Again, the code I use to set it is as follows:
Code: Select all
<?php
session_name('myForm');
session_start();
//phpinfo();
if(isset($_POST['submit'])){
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
}
var_dump($_POST);
var_dump($_SESSION);
Posted: Fri Mar 10, 2006 5:50 am
by s.dot
i doubt this will make any difference, but try
$_SESSION["$key"] = $val <-- using the double quotes
Posted: Fri Mar 10, 2006 5:53 am
by newmember
just for now drop this line:
session_name('myForm');
ALSO try to read what i'm writing...
just before loop through $_POST, do var_dump($_POST) just to see what you get there...
maybe your $_POST is empty then obviously $_SESSION will be empty too
Posted: Fri Mar 10, 2006 5:58 am
by 5leander5
When I do the var_dump($_POST) before the loop, it gives the same correct result. All the elements of the array are populated with the form entries. For some reason however, the $_SESSION array is not being populated.
Posted: Fri Mar 10, 2006 6:05 am
by newmember
check if you are getting inside the loop:
if(isset($_POST['submit'])){
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
}
Posted: Fri Mar 10, 2006 6:21 am
by 5leander5
Good man. It was not getting inside the loop at all.
I have sorted that out and now the $_SESSION['Name'] is set in this script.
However, this is still not being read in my form. The code is as follows:
Code: Select all
<input name="Name" type="text" value="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" size="29">
Any suggestions?
Posted: Fri Mar 10, 2006 6:24 am
by 5leander5
Do I need to do something to ensure that my form can read the $_SESSION array?
Posted: Fri Mar 10, 2006 6:28 am
by newmember
can you write the entire code once?
Posted: Fri Mar 10, 2006 7:00 am
by 5leander5
My form code is:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="ExpressionInterest" method="post" action="sendmail.php">
<blockquote>
<p align="center"><img src="Formtitle.jpg" width="852" height="286" align="middle"></p>
<p align="center"><strong>REGISTER YOUR INTEREST </strong></p>
<p align="center">If you would like to register your interest in attending this event, please complete and </p>
<p align="center">return this form via fax to Norrie Keane at the following number: +353 (0)93 43887. </p>
<p align="center"><strong>Title </strong>:
<select name="Title" size="1" tabindex="1" dir="ltr">
<option> </option>
<option>Dr.</option>
<option>Mr.</option>
<option>Mrs.</option>
<option>Miss.</option>
</select>
<strong> Name </strong>:<strong>
<input name="Name" type="text" value="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" tabindex="2" size="29">
</strong> <strong>Middle Initial </strong>:
<input name="Initial" type="text" id="Initial" tabindex="3" size="5" maxlength="4">
</p>
</blockquote>
<p align="center"><strong>Surname </strong>:
<input name="Surname" type="text" id="Surname" tabindex="4" size="67">
</p>
<blockquote>
<p align="center"><strong>Position </strong>:
<input name="Position" type="text" id="Position" tabindex="5" size="67">
</p>
<p align="center"><strong> Company </strong>:
<input name="Company" type="text" id="Company" tabindex="6" size="67">
</p>
<p align="center"><strong>Address </strong>:
<input name="Address1" type="text" tabindex="7" size="67">
</p>
<p align="center">
<input name="Address2" type="text" tabindex="8" size="67">
</p>
</blockquote>
<blockquote>
<div align="center"></div>
<div align="center">
<input name="Address3" type="text" tabindex="9" dir="ltr" size="67">
</div>
</blockquote>
<p align="center"> <strong>Country </strong>:
<input name="Country" type="text" tabindex="10" size="67">
</p>
<p align="center"><strong> Contact No </strong>:
<input name="ContactNumber" type="text" tabindex="11" size="67">
</p>
<p align="center">(Please include country code)
</p>
<p align="center"><strong>Cell Phone </strong>:
<input name="CellNumber" type="text" tabindex="12" size="67">
</p>
<p align="center">(Please include country code)
</p>
<p align="center"><strong>Email </strong>:
<input name="Email" type="text" tabindex="13" size="67">
</p>
<p align="center"> </p>
<p align="center">By completing this form, you will receive regular, updated Forum information, via email, </p>
<p align="center">as it becomes available, and an official invitation and information pack will be forwarded </p>
<p align="center"> to you via post in due course. </p>
<p align="center">
<input name="Submit" type="submit" tabindex="14" value="Submit">
<input name="Reset" type="reset" tabindex="15" value="Reset">
</p>
</form>
<p align="center">___________________________________________________________________________________ </p>
<p align="center">Moyne Park, Tuam, Co. Galway, Ireland </p>
<p align="center">Telephone: +353 (0)93 43900 Facsimile: +353 (0)93 43887 Email: nkeane@rivada.com </p>
<p> </p>
</body>
</html>
Is this what you mean?
My script code is:
Code: Select all
<?php
session_name('myForm');
session_start();
//phpinfo();
if(isset($_POST['Submit'])){
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
}
//var_dump($_POST);
//var_dump($_SESSION['Name']);
$email = $_POST['Email'];
$body = $_POST['Title']."\n";
$body .= $_POST['Name']."\n";
$body .= $_POST['Initial']."\n";
$body .= $_POST['Surname']."\n";
$body .= $_POST['Position']."\n";
$body .= $_POST['Company']."\n";
$body .= $_POST['Address1']."\n";
$body .= $_POST['Address2']."\n";
$body .= $_POST['Address3']."\n";
$body .= $_POST['Country']."\n";
$body .= $_POST['ContactNumber']."\n";
$body .= $_POST['CellNumber']."\n";
$body .= $_POST['Email']."\n";
mail( "albert_oflaherty@hotmail.com", "Expression Of Interest in Forum on Public Safety 2006", $body, "From: $email" );
header( "Location: http://www.bertsparadise.com/ExpressionOfInterest3.php" );
?>