43 Replies - 833 Views - Last Post: 25 July 2011 - 03:44 PM
#31
Re: Validation Doesn't Work
Posted 25 July 2011 - 01:27 PM
I'm confused on the way you checked for errors. Why did you set the default values for $name, $email, $subject, and $message? The reason I ask is because in the mail function you didn't even use these variables. Basically they're initialized and have a value set to them but they are never used. Instead of using them you used $_POST['name'] and so on.
Also in the beginning of the validation you used isset($_POST['submit']). Why?
#32
Re: Validation Doesn't Work
Posted 25 July 2011 - 01:31 PM
<input type="text" id="name" name="name" value="<?php echo $name ?>" size="50">
You could change the code to use these variables more, but didn't see the point. You could also remove them altogether, and use something like this, for the value:
<input type="text" id="name" name="name" value="<?php echo ( isset($_POST['name']) ? $_POST['name'] : '' ) ?>" size="50">
.. but it's a bit longer
It checks for $_POST['submit'] to detect whether the form has been submitted or not. You could instead check for the request type, but this ensures it's actually a form submission and not just a random POST request.
#33
Re: Validation Doesn't Work
Posted 25 July 2011 - 01:45 PM
RudiVisser, on 25 July 2011 - 03:31 PM, said:
I don't think I understand what you mean. Can you explain it a different way please?
RudiVisser, on 25 July 2011 - 03:31 PM, said:
Ok, that makes sense!
#34
Re: Validation Doesn't Work
Posted 25 July 2011 - 01:47 PM
<input type="text" id="name" name="name" value="<?php echo $POST['name'] ?>" size="50">
.. then if the form wasn't submitted PHP would output an undefined index error, as $_POST['name'] wouldn't exist.
By setting the default value of $name to a blank string, and populating it when the form is submitted, there's no undefined variables and the output will always either be a blank string or the submitted value.
#35
Re: Validation Doesn't Work
Posted 25 July 2011 - 01:54 PM
|| !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))What is this? FILTER_VALIDATE_EMAIL looks like a function but you never created it.
Finally, may I use the corrections that you made to my code in my website?
This post has been edited by RandomlyKnighted: 25 July 2011 - 01:55 PM
#36
Re: Validation Doesn't Work
Posted 25 July 2011 - 01:58 PM
You can view a list of the filter constants here, and all possible constants for the filter_var function via the categories seen here.
You are of course welcome to use my code on your site, I hope it helps/helped you to learn something
#37
Re: Validation Doesn't Work
Posted 25 July 2011 - 02:14 PM
This post has been edited by RandomlyKnighted: 25 July 2011 - 02:14 PM
#38
Re: Validation Doesn't Work
Posted 25 July 2011 - 02:23 PM
#39
Re: Validation Doesn't Work
Posted 25 July 2011 - 02:52 PM
#40
Re: Validation Doesn't Work
Posted 25 July 2011 - 02:57 PM
This example below will use another variable for simplicity.
<?php
// Define empty error array for when we haven't submitted the form
$errors = array();
// We haven't had a successful submission so set it to false
$success = false;
// 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);
// Success, admin emailed!
$success = true;
}
}
?>
<!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 we were successful
if ($success) { ?>
<h1>Success!</h1>
<p>Message sent.</p>
<?php } else {
// If we weren't successful, form submitted or not
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>
<?php } ?>
</div>
<div id="footer">
<p>
Copyright © 2011 - All Rights Reserved
</p>
</div>
</div>
</div>
</div>
</body>
</html>
#41
Re: Validation Doesn't Work
Posted 25 July 2011 - 03:11 PM
#42
Re: Validation Doesn't Work
Posted 25 July 2011 - 03:40 PM
size=
in the input field. I'm working now to figure out why it does this. Hopefully I figure out a solution soon.
#43
Re: Validation Doesn't Work
Posted 25 July 2011 - 03:41 PM
#44
Re: Validation Doesn't Work
Posted 25 July 2011 - 03:44 PM
|
|

New Topic/Question
Reply





MultiQuote



|