Jump to content

Problem with isset on Form


doubledee

Recommended Posts

I have a form that is supposed to use PHP to display errors next to incomplete form fields when the form is submitted.

 

But I am never getting into my PHP loop.

 

I tried using the print_r(true) function to debug, but no luck.

 

When I click submit I see this i the URL...

 

http://localhost/00_MyWebSite/zzEmptyPHP.php?firstName=&lastName=&submit=Process+Order&submitted=true

 

which is what I would expect on a blank form being submitted.

 

Here is my code...

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link type="text/css" rel="stylesheet" href=".css">
	<style type="text/css" >
		form{
			width: 400px;
			margin: 0 auto;
		}
	</style>
</head>
<body>
	<?php
		// Initialize.
		$errors = array();
		$errors['firstName'] = '';
		$errors['lastName'] = '';
		$firstName = $lastName = '';

		echo "print_r = " . print_r(true);

		if (isset($_POST['submit'])){
			// Handle Form.

			// Trim all incoming data.
			$trimmed = array_map('trim', $_POST);

			// Check First Name.
			if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstName'])){
				$firstName = $_POST['firstNamae'];
			}else{
				$errors['firstName'] = 'Please enter your First Name.';
			}

			// Check Last Name.
			if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['lastName'])){
				$lastName = $_POST['lastName'];
			}else{
				$errors['lastName'] = 'Please enter your Last Name.';
			}

			// if there are errors then go back to the form and display them
		}else{

		}
	?>
	<form action="">
		<fieldset>
			<legend>Billing Details</legend>
			<ol>
				<li>
					<label for="firstName">First Name:</label>
					<input id="firstName" name="firstName" class="text" type="text" />
					<?php echo $errors['firstName']; ?>
				</li>
				<li>
					<label for="lastName">Last Name:</label>
					<input id="lastName" name="lastName" class="text" type="text" />
					<?php echo $errors['lastName']; ?>
				</li>
			</ol>
			<input type="submit" name="submit" value="Process Order" />
			<input type="hidden" name="submitted" value="true" />
		</fieldset>
	</form>
</body>
</html>

 

 

Any ideas what I am doing wrong?

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

Like I said before, values appended to the end of a URL (web address) as a query string are being passed via GET.

 

Let's say you have a URL like www.example.com/index.php?firstname=forrest&lastname=gump

 

The '?' denotes the beginning of a query string.  Query strings contain name/value pairs.  So, as you can see from this example, there are two pairs - firstname, which has the value forrest, and lastname, which has the value gump.  To grab a hold of these in PHP, you'd need to access them through the $_GET superglobal array, like so:

 

echo $_GET['firstname'];

 

POST is different in that the data sent is passed in the background.  There's no query string with POST.

 

To submit the form as POST, you need to specify it as the value of the method attribute of your form tag:

 

<form action="" method="post">

 

Just be aware, like I said above, when you're passing data via POST, your address will simply be http://localhost/00_MyWebSite/zzEmptyPHP.php.  You won't have a query string.

Link to comment
Share on other sites

POST is different in that the data sent is passed in the background.  There's no query string with POST.

 

Sorry, I left my brain somewhere else today!  (I knew that.)  :shy:

 

 

To submit the form as POST, you need to specify it as the value of the method attribute of your form tag:

 

<form action="" method="post">

 

Oops,I didn't notice that.

 

Boy, those were two dumb mistakes!  (Worse than forgetting a semi-colon!)

 

Thanks for the help!

 

 

 

Debbie

 

 

Link to comment
Share on other sites

A few more questions while I am at it...

 

1.) Why do I see this on my screen??

 

1print_r = 1

 

Where are those 1's coming from?

 

I have this code...

<body>
	<?php
		// Initialize.
		$errors = array();
		$errors['firstName'] = '';
		$errors['lastName'] = '';
		$firstName = $lastName = '';

		echo "print_r = " . print_r(true);

		if (isset($_POST['submit'])){
			// Handle Form.

 

 

2.) Do I need to put anything else in the form tag, or is this okay?

 

	<form action="" method="post">

 

 

3.) Is it okay to submit a form back on to itself?

 

The book I read said that is cleaner than having to deal with two files.

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

1. It comes from this line:

 

echo "print_r = " . print_r(true);

 

True can also be represented as 1.  In binary, 0 is false/off while 1 is true/on.  As a bit of trivia, that's why power switches have a 1 superimposed over a 0 as their symbol.

 

2. Yes, it will cause the form to submit to itself via POST.

 

3. It depends on what you need to do.  Forms that submit to themselves are pretty common as it makes it easy to handle/display form field errors.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.