Hi all!
I am working on a simple eCard system. It consists of 2 pages thus far. I need to preserve which card was chosen while delivering form errors such as “not a valid email address”, “empty first name”, etc…having trouble.
Page 1 - you choose the card you want to send (this is stored in a session)
Page 2 - You fill out the details such as recipient’s email address, first and last name
My problem:
If on page 1 you don’t choose a card - you’re taken to page 2 where the form intentionally does not display. Instead an error message that says “You didn’t pick a card - please go back” displays.
If you pick a card, great! the form displays and because of the session the appropriate card is also displayed and remembered. But whenever I try to validate email address, whether the first or last name fields have been filled out, this 2nd page is reloaded and it says “You haven’t chosen a card”…I somehow can’t maintain session state and do form validation.
Here’s the script…
Page 1:
[php]if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
if(isset($_POST["card"])) {
session_start();
$_SESSION["card"] == $_POST["card"];
} else {}
} else { }
[/php]
Page 2:
[php]if(isset($_POST[“card”])) {
echo “
Step 2: Customize Your Card
”; session_start();
// display correct image for card chosen
if($_POST["card"] == 'card-one') {
echo '<img src="images/card-one.png" align="right" />';
} elseif($_POST["card"] == 'card-two') {
echo '<img src="images/card-two.png" align="right" />';
} elseif($_POST["card"] == 'card-three') {
echo '<img src="images/card-three.jpg" align="right" />';
} elseif($_POST["card"] == 'card-four') {
echo '<img src="images/card-four.jpg" align="right" />';
} elseif($_POST["card"] == 'card-five') {
echo '<img src="images/card-five.jpg" align="right" />';
} elseif($_POST["card"] == 'card-six') {
echo '<img src="images/card-six.jpg" align="right" />';
} elseif($_POST["card"] == 'card-seven') {
echo '<img src="images/card-seven.jpg" align="right" />';
} elseif($_POST["card"] == 'card-eight') {
echo '<img src="images/card-eight.jpg" align="right" />';
} elseif($_POST["card"] == 'card-nine') {
echo '<img src="images/card-nine.jpg" align="right" />';
} elseif($_POST["card"] == 'card-ten') {
echo '<img src="images/card-ten.jpg" align="right" />';
} elseif($_POST["card"] == 'card-eleven') {
echo '<img src="images/card-eleven.jpg" align="right" />';
} elseif($_POST["card"] == 'card-twelve') {
echo '<img src="images/card-twelve.jpg" align="right" />';
}
echo "<form action=\"\" method=\"get\" name=\"details\"><p>Your Name:*</p> <p><input type=\"text\" name=\"sendersName\" size=\"30\" \></p>";
echo "<p>Recipient's First Name:* </p><p><input type=\"text\" name=\"recipientsFirstName\" size=\"30\" \></p>";
echo "<p>Recipient's Last Name:* </p><p><input type=\"text\" name=\"recipientsLastName\" size=\"30\" \></p>";
echo "<p>Recipient's Email:* </p><p><input type=\"text\" name=\"recipientsEmail\" size=\"30\" \></p>";
echo "<p><input type=\"submit\" id=\"submit\" value=\"Next\" /></p>";
echo "</form>";
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(empty($_POST['sendersName'])) {
echo "Please enter your name";
} elseif(empty($_POST['recipientsFirstName'])) {
echo "Please fill out the recipient's first name";
} elseif(empty($_POST['recipientsLastName'])) {
echo "Please fill out the recipient's last name";
} elseif(empty($_POST['recipientsEmail'])) {
echo "Please fill out the recipient's email address";
}
} else {
}
} else {
echo "<h2>No Card Selected</h2>";
echo "You didn't select a card. Please go back and select one <a href=\"step-1.php \">Here</a>";
}[/php]
Note: I didn’t validate everything that you could and taken the precautionary security measures, just because I can’t get the basics to work out.