setting $_SESSION variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

setting $_SESSION variables

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

It looks good to me. Are you getting any error messages?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post 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.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post 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...
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post 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?
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post 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 :P
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post 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);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

i doubt this will make any difference, but try

$_SESSION["$key"] = $val <-- using the double quotes
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post 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
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post 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.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

check if you are getting inside the loop:
if(isset($_POST['submit'])){
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
}
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post 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?
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

Do I need to do something to ensure that my form can read the $_SESSION array?
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

can you write the entire code once?
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post 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. &nbsp; </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>    
           &nbsp;<strong> Name </strong>:<strong>
       <input name="Name" type="text" value="<?php if(isset($_SESSION['Name'])){
		echo $_SESSION['Name']; }?>" tabindex="2" size="29">
           </strong> &nbsp;<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>:
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Surname" type="text" id="Surname" tabindex="4" size="67">
              </p>
       <blockquote>
         <p align="center"><strong>Position </strong>:&nbsp; 
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Position" type="text" id="Position" tabindex="5" size="67">
                    </p>
         <p align="center"><strong> Company </strong>: 
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Company" type="text" id="Company" tabindex="6" size="67">
                  </p>
         <p align="center"><strong>Address </strong>: 
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Address1" type="text" tabindex="7" size="67">
                  </p>
         <p align="center">   &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           <input name="Address2" type="text" tabindex="8" size="67">
                  </p>
       </blockquote>
       <blockquote>
         <div align="center"></div>
         <div align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           <input name="Address3" type="text" tabindex="9" dir="ltr" size="67">
         </div>
       </blockquote>
       <p align="center">    <strong>Country </strong>:       
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Country" type="text" tabindex="10" size="67">
  </p>
  <p align="center"><strong> Contact No </strong>:
    &nbsp;&nbsp;<input name="ContactNumber" type="text" tabindex="11" size="67">
  </p>
  <p align="center">(Please include country code)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
  <p align="center"><strong>Cell Phone </strong>: 
    &nbsp;&nbsp;&nbsp;&nbsp;<input name="CellNumber" type="text" tabindex="12" size="67">
  </p>
  <p align="center">(Please include country code)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
  <p align="center"><strong>Email </strong>:
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="Email" type="text" tabindex="13" size="67">
  </p>
  <p align="center">&nbsp; </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">&nbsp;to you via post in due course. </p>
  <p align="center">
    <input name="Submit" type="submit" tabindex="14" value="Submit">
    &nbsp;&nbsp;&nbsp;&nbsp;<input name="Reset" type="reset" tabindex="15" value="Reset">
  &nbsp;&nbsp;&nbsp; </p>
</form>
<p align="center">___________________________________________________________________________________ </p>
<p align="center">Moyne Park, Tuam, Co. Galway, Ireland &nbsp; </p>
<p align="center">Telephone: +353 (0)93 43900 Facsimile: +353 (0)93 43887 &nbsp; Email: nkeane@rivada.com </p>
<p>&nbsp;</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" );
?>
Post Reply