<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" href="style.css" rel="stylesheet" />
<title>Twickenham-Milford Chapter :: Contact Us</title>
</head>
<body>
<div id="container">
<div id="banner">
<img src="banner.png" alt="Banner" />
</div>
<div id="NavContentContainer">
<div id="LeftNav">
<ul>
<li> </li>
<li> </li>
<li>
<a href="http://demolay.asissweb.com">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="officers.html">Leaders</a>
</li>
<li>
<a href="resources.html">Resources</a>
</li>
<li>
<a href="contact.html">Contact Us</a>
</li>
<li>
<a href="login.html">Log In</a>
</li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
</ul>
</div>
<div id="ContentContainer">
<div id="mainContent">
<br /> <br /> <br /> <br />
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "webmaster@asissweb.com";
$header = "From: " . $name . " < " . $email . ">";
if (empty($name) || empty($subject) || empty($email) || empty($message))
{
if (empty($name))
{
echo 'Please enter your name into the space provided. <br />';
}
if (empty($subject))
{
echo 'Please enter a subject into the space provided. <br />';
}
if (empty($email))
{
echo 'Please enter your email address into the space provided. <br />';
}
if (empty($message))
{
echo 'Please enter your message into the space provided. <br />';
}
echo '<form method="post" action="contact.php" style="margin-left:20px;">';
echo '<label for="name">Name:</label>';
echo '<input type="text" id="name" name="name" size="50">';
echo '<br />';
echo '<br />';
echo '<label for="email">E-mail:</label>';
echo '<input type="text" id="email" name="email" size="50" >';
echo '<br />';
echo '<br />';
echo '<label for="subject">Subject:</label>';
echo '<input type="text" id="subject" name="subject" size="50">';
echo '<br />';
echo '<br />';
echo '<label for="message">Message:</label>';
echo '<br />';
echo '<br />';
echo '<textarea rows="5" cols="50" name="message">';
echo '</textarea>';
echo '<br />';
echo '<br />';
echo '<input value="Submit" type="submit">';
echo '</form>';
}
else
{
mail($to, $subject, $message, $header);
header("Location: success.html");
}
?>
</div>
<div id="footer">
<p>
Copyright © 2011 - All Rights Reserved
</p>
</div>
</div>
</div>
</div>
</body>
</html>
43 Replies - 810 Views - Last Post: 25 July 2011 - 03:44 PM
#1
Validation Doesn't Work
Posted 21 July 2011 - 08:42 PM
Replies To: Validation Doesn't Work
#2
Re: Validation Doesn't Work
Posted 21 July 2011 - 10:19 PM
#3
Re: Validation Doesn't Work
Posted 21 July 2011 - 10:24 PM
#4
Re: Validation Doesn't Work
Posted 22 July 2011 - 12:08 AM
This post has been edited by thephpdev: 22 July 2011 - 12:08 AM
#5
Re: Validation Doesn't Work
Posted 22 July 2011 - 03:56 AM
#6
Re: Validation Doesn't Work
Posted 22 July 2011 - 04:35 AM
#7
Re: Validation Doesn't Work
Posted 22 July 2011 - 06:01 AM
1. Having two copies of your form is a bad idea. You will inevitably make changes to one that you forget to reflect in the other. You could take the above code and, with a few changes, make it the only place that has the actual form. Your choice, ultimately, but code duplication is bad bad bad.
2. When you pull out your post vars, you should probably trim the inputs. Otherwise, if the person hits space or something, it'll register as a valid input.
$name = trim($_POST['name']);
That may fix your problem, or maybe not.
3. Instead of empty, you could use strlen.
if (strlen($name) == 0
I'm not saying that empty is wrong... it should work. But this is a different path just in case variables coming out of the $_POST superglobal are created funny.
#8
Re: Validation Doesn't Work
Posted 22 July 2011 - 08:27 AM
You should refactor your code into something more concise and understandable such as the following. It will consolidate your code and make it much easier to understand.
This page can be used as your default contact form in all instances, so just link straight to this page and it will show the form with no errors (unlike your old one..) on first pageload, then do error checking only on subsequent requests. There is no way that this could redirect without the form being correct, oh and I added checking for a valid email.
<?php
// Define empty error array for when we haven't submitted the form
$errors = array();
if (isset($_POST['submit'])) {
// If the form is posted, check for errors
if (empty($_POST['name']))
$errors[] = 'Please enter your name into the space provided.<br />';
if (empty($_POST['subject']))
$errors[] = 'Please enter a subject into the space provided.<br />';
if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
$errors[] = 'Please enter a valid email address into the space provided.<br />';
if (empty($_POST['message']))
$errors[] = 'Please enter a message into the space provided.<br />';
if (empty($errors)) {
// If we have no errors, email and redirect as appropriate..
$to = 'webmaster@asissweb.com';
$header = 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>';
mail($to, $_POST['subject'], $_POST['message'], $header);
header("Location: success.html");
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link type="text/css" href="style.css" rel="stylesheet"/>
<title>Twickenham-Milford Chapter :: Contact Us</title>
</head>
<body>
<div id="container">
<div id="banner">
<img src="banner.png" alt="Banner"/>
</div>
<div id="NavContentContainer">
<div id="LeftNav">
<ul>
<li> </li>
<li> </li>
<li>
<a href="http://demolay.asissweb.com">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="officers.html">Leaders</a>
</li>
<li>
<a href="resources.html">Resources</a>
</li>
<li>
<a href="contact.html">Contact Us</a>
</li>
<li>
<a href="login.html">Log In</a>
</li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
</ul>
</div>
<div id="ContentContainer">
<div id="mainContent">
<br/> <br/> <br/> <br/>
<?php
if (!empty($errors))
foreach ($errors as $error)
echo $error;
?>
<form method="post" action="contact.php" style="margin-left:20px;">
<label for="name">Name:</label>
<input type="text" id="name" name="name" size="50">
<br/>
<br/>
<label for="email">E-mail:</label>
<input type="text" id="email" name="email" size="50">
<br/>
<br/>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" size="50">
<br/>
<br/>
<label for="message">Message:</label>
<br/>
<br/>
<textarea rows="5" cols="50" name="message">
</textarea>
<br/>
<br/>
<input value="Submit" type="submit" name="submit">
</form>
</div>
<div id="footer">
<p>
Copyright © 2011 - All Rights Reserved
</p>
</div>
</div>
</div>
</div>
</body>
</html>
Just a mini disclaimer, this isn't properly tested per se (there's no errors but still untested!), but it should be easy enough to figure out if anything's wrong.
EDIT: Also on another note, you shouldn't be doing things like <li> </li> or even [li]<br/> <br/> <br/> <br/>[/li], there's no sense in that. Instead try to work your page's layout out completely in HTML.
As another aside, your errors should just be text, and then proceed to output them in the foreach loop as individual <li> elements in a <ul>. It all helps to producing more semantic, readable code that makes sense.
This post has been edited by RudiVisser: 22 July 2011 - 08:30 AM
#9
Re: Validation Doesn't Work
Posted 23 July 2011 - 09:35 AM
Quote
The reason I'm having 2 copies of my form is because I am following the Head First PHP & MySQL book as I work on this website. To me it seems like a good idea if I can just get it so that when it gives the error it also displays the user's old message so they don't have to type it again.
I don't know what was wrong the other day, but right now the validation works and it displays the error message it just doesn't keep the user's information that they entered.
#10
Re: Validation Doesn't Work
Posted 23 July 2011 - 09:54 AM
#11
Re: Validation Doesn't Work
Posted 23 July 2011 - 09:59 AM
This post has been edited by RandomlyKnighted: 23 July 2011 - 10:00 AM
#12
Re: Validation Doesn't Work
Posted 23 July 2011 - 10:10 AM
If it's teaching you bad practice like this, especially in the early stages, it's a terrible terrible! book.
The way that I demonstrated to do it would be a perfectly understandable and much better way to structure your code, I also explained a few key reasons as to why it's better than your current code which should help you to understand why I did it like that.
To achieve your goal of having it store the form values you certainly do not need a second form, this is essentially duplicating code and is again, bad practice. You can easily use the code that I am demonstrating below to have everything contained within a single page, and with a slight bit of extension, you can even put the contents of success.phtml in there.
The code below will define default values for each of your form elements (ie. blank when it's not posted) and populate them if they exist.
<?php
// Define empty error array for when we haven't submitted the form
$errors = array();
// Define default values for each form field
$name = $subject = $email = $message = '';
if (isset($_POST['submit'])) {
// If the form is posted, check for errors
if (empty($_POST['name']))
$errors[] = 'Please enter your name into the space provided.<br />';
if (empty($_POST['subject']))
$errors[] = 'Please enter a subject into the space provided.<br />';
if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
$errors[] = 'Please enter a valid email address into the space provided.<br />';
if (empty($_POST['message']))
$errors[] = 'Please enter a message into the space provided.<br />';
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($errors)) {
// If we have no errors, email and redirect as appropriate..
$to = 'webmaster@asissweb.com';
$header = 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>';
mail($to, $_POST['subject'], $_POST['message'], $header);
header("Location: success.html");
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link type="text/css" href="style.css" rel="stylesheet"/>
<title>Twickenham-Milford Chapter :: Contact Us</title>
</head>
<body>
<div id="container">
<div id="banner">
<img src="banner.png" alt="Banner"/>
</div>
<div id="NavContentContainer">
<div id="LeftNav">
<ul>
<li> </li>
<li> </li>
<li>
<a href="http://demolay.asissweb.com">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="officers.html">Leaders</a>
</li>
<li>
<a href="resources.html">Resources</a>
</li>
<li>
<a href="contact.html">Contact Us</a>
</li>
<li>
<a href="login.html">Log In</a>
</li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
<li> </li>
</ul>
</div>
<div id="ContentContainer">
<div id="mainContent">
<br/> <br/> <br/> <br/>
<?php
if (!empty($errors))
foreach ($errors as $error)
echo $error;
?>
<form method="post" action="contact.php" style="margin-left:20px;">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $name ?>" size="50">
<br/>
<br/>
<label for="email">E-mail:</label>
<input type="text" id="email" name="email" name="<?php echo $email ?> size="50">
<br/>
<br/>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject ?> size="50">
<br/>
<br/>
<label for="message">Message:</label>
<br/>
<br/>
<textarea rows="5" cols="50" name="message"><?php echo $message ?></textarea>
<br/>
<br/>
<input value="Submit" type="submit" name="submit">
</form>
</div>
<div id="footer">
<p>
Copyright © 2011 - All Rights Reserved
</p>
</div>
</div>
</div>
</div>
</body>
</html>
EDIT: Another reason to do it this way rather than the initial way, what happens when a user visits the submission page without POSTing any data? It will show some nasty errors to the user which is never expected nor good.
This post has been edited by RudiVisser: 23 July 2011 - 10:16 AM
#13
Re: Validation Doesn't Work
Posted 23 July 2011 - 11:26 AM
contact.php
<?php
session_start();
$inputs = array('name'=>'','subject'=>'','email'=>'','message'=>'');
function check_input(&$data) {
$expected = array('name','subject','email','message');
$error = array('Please enter your name into the space provided. <br />', 'Please enter a subject into the space provided. <br />', 'Please enter your email address into the space provided. <br />', 'Please enter your message into the space provided. <br />');
$received = array();
$errors = "";
for($i = 0; $i < 4; $i++) {
if(empty($_POST[$expected[$i]])) {
$errors .= $error[$i];
} else {
$data[$expected[$i]] = $_POST[$expected[$i]];
}
}
return $errors;
}
if(isset($_SESSION['errorcheck'])) {
$out = check_input($inputs);
} else {
$out = '';
}
if($out != '' || !isset($_SESSION['errorcheck'])) {
$_SESSION['errorcheck'] = 1;
$out .='<form method="post" action="contact.php" style="margin-left:20px;">';
$out .='<label for="name">Name:</label>';
$out .='<input type="text" id="name" name="name" size="50" value="'.$inputs['name'].'">';
$out .='<br />';
$out .='<br />';
$out .='<label for="email">E-mail:</label>';
$out .='<input type="text" id="email" name="email" size="50" value="'.$inputs['email'].'">';
$out .='<br />';
$out .='<br />';
$out .='<label for="subject">Subject:</label>';
$out .='<input type="text" id="subject" name="subject" size="50" value="'.$inputs['subject'].'">';
$out .='<br />';
$out .='<br />';
$out .='<label for="message">Message:</label>';
$out .='<br />';
$out .='<br />';
$out .='<textarea rows="5" cols="50" name="message" >';
$out .= $inputs['message'].'</textarea>';
$out .='<br />';
$out .='<br />';
$out .='<input value="Submit" type="submit">';
$out .='</form>';
echo str_replace("PHPOUTPUT", $out, file_get_contents("knighted.html"));
} else {
session_destroy();
echo "mail to..."; // while debugging, no need to mail and redirect...
//mail($to, $subject, $message, $header);
//header("Location: success.html");
}
?>
knighted.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link type="text/css" href="style.css" rel="stylesheet" /> <title>Twickenham-Milford Chapter :: Contact Us</title> </head> <body> <div id="container"> <div id="banner"> <img src="banner.png" alt="Banner" /> </div> <div id="NavContentContainer"> <div id="LeftNav"> <ul> <li> </li> <li> </li> <li> <a href="http://demolay.asissweb.com">Home</a> </li> <li> <a href="about.html">About</a> </li> <li> <a href="officers.html">Leaders</a> </li> <li> <a href="resources.html">Resources</a> </li> <li> <a href="contact.html">Contact Us</a> </li> <li> <a href="login.html">Log In</a> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> </ul> </div> <div id="ContentContainer"> <div id="mainContent"> <br /> <br /> <br /> <br /> PHPOUTPUT </div> <div id="footer"> <p> Copyright © 2011 - All Rights Reserved </p> </div> </div> </div> </div> </body> </html>
I'd probably spend more time separating the HTML from the PHP to make it easier to debug later, but this is a start.
This post has been edited by CTphpnwb: 23 July 2011 - 11:42 AM
#14
Re: Validation Doesn't Work
Posted 23 July 2011 - 09:21 PM
RudiVisser, on 23 July 2011 - 12:10 PM, said:
CTphpnwb, on 23 July 2011 - 01:26 PM, said:
The next few pages teach a better way of doing this. Basically the book teaches a right way and a wrong way to go about doing all this. So please next time, don't tell someone to stop reading the book if you don't know how the book actually teaches. If I had followed your advice then I would have spent another $50 for a book when I really don't need to.
I'll work on revising my code when I get the chance and will post back if I need further help with this issue.
#15
Re: Validation Doesn't Work
Posted 24 July 2011 - 04:16 AM
In fact, the book then moves straight onto PHP & MySQL toolbox, whatever that is. It's giving me a headache now.
Can you demonstrate what the "good" method is? I have seen no statement made saying that it's bad.
Nowhere does it suggest that you should check if the form is posted before using the $_POST variables, it just uses them. I find it quite funny that in the first few pages it says "Assume everyone is out to get you", and then they do this? Anyone can spam an email to you with this code. Actually I just read through the security section aswell (well, scanned through!) and all it really teaches you is how to use basic authentication and ignore values that aren't approved by an admin.
It does not teach parameterized queries or anything that you'll need for the real world
Anyway I'm giving up now.
This post has been edited by RudiVisser: 24 July 2011 - 04:22 AM
|
|

New Topic/Question
Reply




MultiQuote






|