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!
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:
<?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>
<?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!
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).
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?
<?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>
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?
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.
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?