Page 1 of 1

Help with passing variables.

Posted: Wed Aug 05, 2009 6:19 am
by dissonantallure
I keep losing variables every time I try to pass them across more than 2 pages. I have tried a million different ways and still cannot find a solution. Here is my example below:

Pass.php

Code: Select all

 
<!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=utf-8" />
<title>SC Form Version 1.0</title>
<script type="text/javascript" src="SCscript.js"></script>
<link rel="stylesheet" type="text/css" href="SCstyle.css" />
</head>
<body>
<form name="SCform" method="post" action="pass2.php">
<input type="hidden" name="Pass1" />
<b>Name:</b><input name="Name" type="text" />
<br />
<b>Email:</b><input name="Email" type="text" />
<br />
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
 
Pass2.php

Code: Select all

 
<?php
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
$Pass1 = $_POST['Pass1'];
$Pass3 = $_POST['Pass3'];
// Create an empty array to hold the error messages.
$SC_Errors = array();
// If the email form input "Name" is empty 
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
// If the email form input "Email" is empty
if ($sender_email=='')
// Define a variable for the error message
$SC_Errors['Email'] = 'Enter Your Email Address.';
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if (isset($Pass1)) {
if (count($SC_Errors) == 0) {
include 'pass4.php';
} else {
include 'pass3.php';
}
}
 
 
if (isset($Pass3)) {
if (count($SC_Errors) == 0) {
include 'pass4.php';
} else {
include 'pass3.php';
}
}
 
 
?>
</body>
</html>
 
Pass3.php

Code: Select all

 
<?php
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
$Pass3 = $_POST['Pass3'];
// Create an empty array to hold the error messages.
$SC_Errors = array();
 
// If the email form input "Name" is empty 
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
// If the email form input "Email" is empty
if ($sender_email=='')
// Define a variable for the error message
$SC_Errors['Email'] = 'Enter Your Email Address.';
 
?>
 
<form name="SCform" method="post" action="pass2.php">
<input type="hidden" name="Pass3" />
<!-- Input: Name -->
<table> 
<tr>
<td>
<table>
 <tr>
  <td>
   <b>Name:</b>
  </td>
  <td>
  <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Name" type="text" value="<?php echo $_POST['Name'];?>" />
   </div>
  </td>
 </tr>
</table>
</td>
</tr>
<tr>
 <td>
 <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCerror"'; ?> align="center">
 <?php if (!empty($SC_Errors['Name'])) echo '<b> '.$SC_Errors['Name'].' </b>'; ?>
 </div>
 </td>
 </tr>
</table>
<!-- Input: Name -->
 
<br />
 
<!-- Input: Email -->
<table>
 <tr>
  <td>
 
<table>
 <tr>
  <td>
   <b>Email: </b>
  </td>
  <td>
   <div<?php if (!empty($SC_Errors['Email'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Email" type="text" value="<?php echo $_POST['Email'];?>"  />
   </div>
  </td>
 </tr>
</table>
 
</td>
</tr>
<tr>
<td>
<div<?php if (!empty($SC_Errors['Email'])) echo ' class="SCerror"'; ?> align="center">
<?php if (!empty($SC_Errors['Email'])) echo '<b> '.$SC_Errors['Email'].' </b>'; ?>
</div>
</td>
</tr>
</table>
<!-- Input: Email -->
 
<br />
<br />
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
</form>
 
Pass4.php

Code: Select all

 
<?php
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
?>
<?php echo $sender_name; ?>
 
<?php echo $sender_email; ?>
 
On Pass4.php I would like ask a new series of questions such as phone number, budget etc..

How would I go about passing across the new variables as well as $sender_name and $sender_email to new page?

Also I have a feeling there is an easier way to achieve my ultimate goal of a mutli-page email form. If anyone has any suggestions on what im doing wrong or how to help it will be greatly appreciated!

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 7:13 am
by turbolemon
Use Sessions http://us2.php.net/manual/en/book.session.php. They persist data on the server-side, in the $_SESSION superglobal. All other runtime data is lost (i.e. variables) at the end of the script execution; unless you are persisting it some other way (e.g. as hidden fields in the subsequent forms).

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 4:38 pm
by dissonantallure
Okay I have already tried to use sessions to no avail. Like i said in my first post I have tried EVERYTHING I just can't seem to make my form work. Okay here is my example using sessions. I keep getting stuck on Pass4.php. Tell me can anyone tell me what I am doing wrong?

Pass.php

Code: Select all

 
<!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=utf-8" />
<title>SC Form Version 1.0</title>
<script type="text/javascript" src="SCscript.js"></script>
<link rel="stylesheet" type="text/css" href="SCstyle.css" />
</head>
<body>
<form name="SCform" method="post" action="pass2.php">
<input type="hidden" name="Pass1" />
<b>Name:</b><input name="Name" type="text" />
<br />
<b>Email:</b><input name="Email" type="text" />
<br />
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
 
Pass2.php

Code: Select all

 
<?php
session_start();
$sender_name = $_SESSION['Name']; // Define Post Variable for 'Name'
$sender_email = $_SESSION['Email']; // Define Post Variable for 'Email'
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
$Pass1 = $_POST['Pass1'];
$Pass3 = $_POST['Pass3'];
$Pass4 = $_POST['Pass4'];
// Create an empty array to hold the error messages.
$SC_Errors = array();
// If the email form input "Name" is empty 
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
// If the email form input "Email" is empty
if ($sender_email=='')
// Define a variable for the error message
$SC_Errors['Email'] = 'Enter Your Email Address.';
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if (isset($Pass1)) {
if (count($SC_Errors) == 0) {
include 'pass4.php';
} else {
include 'pass3.php';
}
}
 
 
if (isset($Pass3)) {
if (count($SC_Errors) == 0) {
include 'pass4.php';
} else {
include 'pass3.php';
}
}
 
if (isset($Pass4)) {
if (count($SC_Errors) == 0) {
include 'pass5.php';
} else {
include 'pass4.php';
}
}
 
?>
</body>
</html>
 
Pass3.php

Code: Select all

 
<?php
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
$Pass3 = $_POST['Pass3'];
// Create an empty array to hold the error messages.
$SC_Errors = array();
 
// If the email form input "Name" is empty 
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
// If the email form input "Email" is empty
if ($sender_email=='')
// Define a variable for the error message
$SC_Errors['Email'] = 'Enter Your Email Address.';
 
?>
 
<form name="SCform" method="post" action="pass2.php">
<input type="hidden" name="Pass3" />
<!-- Input: Name -->
<table> 
<tr>
<td>
<table>
 <tr>
  <td>
   <b>Name:</b>
  </td>
  <td>
  <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Name" type="text" value="<?php echo $_POST['Name'];?>" />
   </div>
  </td>
 </tr>
</table>
</td>
</tr>
<tr>
 <td>
 <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCerror"'; ?> align="center">
 <?php if (!empty($SC_Errors['Name'])) echo '<b> '.$SC_Errors['Name'].' </b>'; ?>
 </div>
 </td>
 </tr>
</table>
<!-- Input: Name -->
 
<br />
 
<!-- Input: Email -->
<table>
 <tr>
  <td>
 
<table>
 <tr>
  <td>
   <b>Email: </b>
  </td>
  <td>
   <div<?php if (!empty($SC_Errors['Email'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Email" type="text" value="<?php echo $_POST['Email'];?>"  />
   </div>
  </td>
 </tr>
</table>
 
</td>
</tr>
<tr>
<td>
<div<?php if (!empty($SC_Errors['Email'])) echo ' class="SCerror"'; ?> align="center">
<?php if (!empty($SC_Errors['Email'])) echo '<b> '.$SC_Errors['Email'].' </b>'; ?>
</div>
</td>
</tr>
</table>
<!-- Input: Email -->
 
<br />
<br />
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
</form>
 
 
Pass4.php <-- Causing me problems

Code: Select all

 
<?php
$sender_name = $_SESSION['Name']; // Define Post Variable for 'Name'
$sender_email = $_SESSION['Email']; // Define Post Variable for 'Email'
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
$sender_budget = $_POST['Budget'];
$sender_phone = $_POST['Phone'];
$Pass4 = $_POST['Pass4'];
// Create an empty array to hold the error messages.
$SC_Errors = array();
 
// If the email form input "Name" is empty 
if ($sender_phone=='')
// Define a variable for the error message
$SC_Errors['Phone'] = 'Enter Your Phone Number.';
// If the email form input "Email" is empty
if ($sender_budget=='')
// Define a variable for the error message
$SC_Errors['Budget'] = 'Enter Your Budget.';
 
?>
 
<?php echo $sender_name; ?>
<br />
<?php echo $sender_email; ?>
<br />
<form name="SCform" method="post" action="pass2.php">
<input type="hidden" name="Pass4" />
<!-- Input: Phone -->
<table> 
<tr>
<td>
<table>
 <tr>
  <td>
   <b>Phone:</b>
  </td>
  <td>
  <div <?php if (!empty($SC_Errors['Phone'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Phone" type="text" value="<?php echo $_POST['Phone'];?>" />
   </div>
  </td>
 </tr>
</table>
</td>
</tr>
<tr>
 <td>
 <div <?php if (!empty($SC_Errors['Phone'])) echo ' class="SCerror"'; ?> align="center">
 <?php if (!empty($SC_Errors['Phone'])) echo '<b> '.$SC_Errors['Phone'].' </b>'; ?>
 </div>
 </td>
 </tr>
</table>
<!-- Input: Phone -->
 
<br />
 
<!-- Input: Budget -->
<table>
 <tr>
  <td>
 
<table>
 <tr>
  <td>
   <b>Budget: </b>
  </td>
  <td>
   <div<?php if (!empty($SC_Errors['Budget'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Budget" type="text" value="<?php echo $_POST['Budget'];?>"  />
   </div>
  </td>
 </tr>
</table>
 
</td>
</tr>
<tr>
<td>
<div<?php if (!empty($SC_Errors['Budget'])) echo ' class="SCerror"'; ?> align="center">
<?php if (!empty($SC_Errors['Budget'])) echo '<b> '.$SC_Errors['Budget'].' </b>'; ?>
</div>
</td>
</tr>
</table>
<!-- Input: Budget -->
 
<br />
<br />
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
</form>
 
Pass5.php

Code: Select all

 
<?php
$sender_name = $_POST['Name']; // Define Post Variable for 'Name'
$sender_email = $_POST['Email']; // Define Post Variable for 'Email'
$sender_phone = $_POST['Phone'];
$sender_budget = $_POST['Budget'];
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
echo $sender_name;
?>
<br />
<?php
echo $sender_email;
?>
<br />
<?php
echo $sender_phone;
?>
<br />
<?php
echo $sender_budget;
?>
</body>
</html>
 
 
On Pass4.php when I submit the form $sender_name and $sender_email are lost. Can anyone tell me why?

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 5:14 pm
by aceconcepts
What's returned to you? pass3 or pass4?

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 5:31 pm
by dissonantallure
Pass4.php is returned to me even when all inputs have text entered. It returns Pass4.php because it reads $sender_name and $sender_email as undefined. I cant figure out why my original variables are undefined when I submit the form on Pass4.php. What do you think might be causing this?

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 5:39 pm
by aceconcepts
Take out the validation in pass2 and output the $sender_name and see what you get.

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 6:11 pm
by dissonantallure
It echoes $sender_name up until the same point, and then it becomes undefined.

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 6:15 pm
by aceconcepts
You mean it echoes the value right?

Also, in pass4 you dont need to re-assign the variables - take them out.

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 6:20 pm
by dissonantallure
Yes, for instance if I entered John on Pass.php it would echo "John" on every page up until I submit the form on Pass4.php. As soon as I click "Submit" on Pass4.php $sender_name becomes undefined.

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 7:31 pm
by aceconcepts
I dont see a submit button for pass4.php

Re: Help with passing variables.

Posted: Wed Aug 05, 2009 8:20 pm
by dissonantallure
Okay I have finally figured out how to pass variables across multiple pages by using the following code.

Pass1.php

Code: Select all

 
<?php 
foreach ($_POST as $key => $val) {
echo '<input type="hidden" name="' . $key . '" value="' . $val . '" />' . "\r\n";
}
?>
 
Now I am stuck on how to validate each field without using any JavaScript. I usually use:

Code: Select all

 
<form action="<?php $_SERVER['PHP_SELF'] ?>" />
 
However, I cannot use that code and pass variables to the next page. Can anyone please help me successfully validate and pass form fields across several pages?