Validation Doesn't Work

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 1394 Views - Last Post: 25 July 2011 - 03:44 PM Rate Topic: -----

#31 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 01:27 PM

Yes, it does make sense.

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?
Was This Post Helpful? 0
  • +
  • -

#32 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 01:31 PM

The blank variables are for when the input field value is output, that uses these variables. It ensures that an undefined variable error is not thrown:
<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.
Was This Post Helpful? 0
  • +
  • -

#33 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 01:45 PM

View PostRudiVisser, on 25 July 2011 - 03:31 PM, said:

The blank variables are for when the input field value is output, that uses these variables.


I don't think I understand what you mean. Can you explain it a different way please?

View PostRudiVisser, on 25 July 2011 - 03:31 PM, said:

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.


Ok, that makes sense!
Was This Post Helpful? 0
  • +
  • -

#34 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 01:47 PM

If I were to simply use the code:
<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.
Was This Post Helpful? 1
  • +
  • -

#35 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 01:54 PM

That makes sense. Thanks, I do; however, have two last questions, you validated the email address using
|| !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

Was This Post Helpful? 0
  • +
  • -

#36 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 01:58 PM

filter_var is the method that does the validating, and FILTER_VALIDATE_EMAIL is simply a constant that defines which filter routine it should be using.

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 :)
Was This Post Helpful? 1
  • +
  • -

#37 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 02:14 PM

It definitely helped! haha I feel like I've had my own personal mentor. :D

This post has been edited by RandomlyKnighted: 25 July 2011 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#38 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 02:23 PM

As long as it makes sense to you and you're learning, I'm glad to have helped ^_^
Was This Post Helpful? 0
  • +
  • -

#39 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 02:52 PM

Is there I can set up my contact form so that if it sends the message the script will display the success message rather than redirecting to a new page?
Was This Post Helpful? 0
  • +
  • -

#40 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 02:57 PM

Of course, simply add another variable or check further down the page if the form has been submitted without errors.

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>&nbsp;&nbsp;&nbsp;</li>
                <li>&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;</li>
                <li>&nbsp;&nbsp;&nbsp;</li>
                <li>&nbsp;&nbsp;&nbsp;</li>
                <li>&nbsp;&nbsp;&nbsp;</li>
                <li>&nbsp;&nbsp;&nbsp;</li>
                <li>&nbsp;&nbsp;&nbsp;</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 &copy 2011 - All Rights Reserved
                </p>
            </div>
        </div>
    </div>
</div>
</body>
</html>


Was This Post Helpful? 1
  • +
  • -

#41 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 03:11 PM

Makes sense! Thanks again!
Was This Post Helpful? 0
  • +
  • -

#42 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 03:40 PM

Hmm, it seems to work good except for one minor bug. If you leave out the subject then when it redirects back to contact.php it puts this

 size=



in the input field. I'm working now to figure out why it does this. Hopefully I figure out a solution soon.
Was This Post Helpful? 0
  • +
  • -

#43 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 03:41 PM

I've missed the closing quote from the value= attribute on the subject and email fields, whoops! :o
Was This Post Helpful? 1
  • +
  • -

#44 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 03:44 PM

Got ya. No problems now.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3