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

User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

if(isset($_POST['Submit'])){
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
}

if you say that $_POST is correctly recieved then try this.(get rid of outer if())
reset($_POST);
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
also instead of writing sdo many   write str_repeat(' ',your_number_here)
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

Is this how I replace the blank spaces:

Code: Select all

<p align="center">(Please include country code) <?php str_repeat('&nbsp;',79);?>
  </p>
This does not include any of the blank spaces.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

5leander5 wrote:Is this how I replace the blank spaces:

Code: Select all

<p align="center">(Please include country code) <?php str_repeat('&nbsp;',79);?>
  </p>
This does not include any of the blank spaces.
yes,that would insert 79 spaces
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

This does not result in any blank spaces at all. I cant see why..... Any ideas?
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

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

Post by 5leander5 »

Is &nbsp the correct syntax for a blank space in PHP? I know it is in html, but not sure about php.
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

Can somebody please tell me what I am doing wrong in my html file.

In my php script, I have set the $_SESSION array successfully. I have verified this.

However, when I read the $_SESSION array in the html page to which the user is directed, it reads the $_SESSION as having a NULL value.

The start of my html file is as follows:

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>
		   <?php 
		   //var_dump($_SESSION['Name']);?>   
       <input name="Name" type="text" value="<?php if(isset($_SESSION['Name'])){
		echo $_SESSION['Name'];}
		else
		echo($_POST['Name']);?>" tabindex="2" size="29">
It is the final few lines which try to read the $_SESSION variable but it returns a NULL.

Any ideas why??????? In despair!!!!!!
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Do you set the session value?

You need to do something like this:

Code: Select all

session_start();
foreach($_POST as $key=>$val){
  $_SESSION[$key] = $val;
}
empty($_POST);
echo "<pre>";
var_dump($_POST);
var_dump($_SESSION);
echo "</pre>";
This should output all the posts and sessions, and give you an idea whats happening wrong.
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

All the $_POST and $_SESSION array elements are set correctly within the script. I have verified this. The script is as follows:

Code: Select all

<?php
session_name('myForm');
session_start();

if(isset($_POST['Submit'])){ 
foreach($_POST as $key => $val) 
{ 
$_SESSION[$key] = $val; 
} 
} 

$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" ); 
empty($_POST);
session_write_close();
header( "Location: http://www.bertsparadise.com/ExpressionOfInterest3.php" );
?>
My problem is that when I try to read the $_SESSION from ExpressionOfInterest3.php, it gives me a NULL value.

I cannot figure out why......
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

are you using

Code: Select all

session_start();
in the second page too?

because you need too :)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

use the PHP tags if your posting php code and code for HTML JAVA,etc
Post Reply