Form Validation While Preserving Session State?

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.

Without seeing the full code on page 1 it’s hard to tell what the problem is, but looking at the code you posted, I’m guessing you’re unnecessarily using a session for something the form should be doing…

Hi - thanks for the reply…

But don’t I need to create a session to pass data from one page to the next?

Here’s the rest of the code it it’s helpful…

[code]



















<input type=“radio” name=“card” value=“card-eleven” /




<?php
			if($_SERVER['REQUEST_METHOD'] == 'POST') {
				
				if(isset($_POST["card"])) {
					session_start();
					$_SESSION["card"] == $_POST["card"];
				} else {}

			} else { }


			?>
		<input type="submit" name="to-step-2" value="Next" id="submit" /></div>
		</form>[/code]

You are using sessions for the wrong reasons. I’d stick to using forms and using the superglobal $_POST. Also, you a switch loop rather than all those elseifs

I’m lost…I converted the if statements to a switch command as you had suggest and completely removed the use of sessions and am relying on just passing form data directly, but it still doesn’t go through the form validation check process and loses track of which card was originally selected.

This is so basic…I know - I just can’t seem to picture how it should work.

[php]

<?php if($_POST["card"]) { switch($_POST["card"]) { case $_POST["card"] == 'card-one': echo ''; break; case $_POST["card"] == 'card-two': echo ''; break; case $_POST["card"] == 'card-three': echo ''; break; case $_POST["card"] == 'card-four': echo ''; break; case $_POST["card"] == 'card-five': echo ''; break; case $_POST["card"] == 'card-six': echo ''; break; case $_POST["card"] == 'card-seven': echo ''; break; case $_POST["card"] == 'card-eight': echo ''; break; case $_POST["card"] == 'card-nine': echo ''; break; case $_POST["card"] == 'card-ten': echo ''; break; case $_POST["card"] == 'card-eleven': echo ''; break; case $_POST["card"] == 'card-twelve': echo ''; break; } else {} echo "

Your Name:*

"; echo "

Recipient's First Name:*

"; echo "

Recipient's Last Name:*

"; echo "

Recipient's Email:*

"; echo ""; echo ""; } else { echo "

No Card Selected

"; echo "You didn't select a card. Please go back and select one Here"; } ?> <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { $senders_name = $_POST['sendersName']; $recs_first_name = $_POST['recipientsFirstName']; $recs_last_name = $_POST['recipientsLastName']; $recs_email = $_POST['recipientsEmail']; if(empty($senders_name)) { echo "Please provide your name"; } elseif(empty($recs_first_name)) { echo "Please provide the recipient's first name"; } elseif(empty($recs_last_name) { echo "Please provide the recipient's last name"; } elseif(empty($recs_email)) { echo "Please provide a valid email address for recipient"; } elseif(filter_var($recs_email, FILTER_VALIDATE_EMAIL)) { echo "Please provide a valid email address for recipient"; } else { $to = $recs_email; $subject = "You Have Received An eCard"; $message = ""; $headers = ""; mail($to, $subject, $message, $headers); } } else {} ?>
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service