PHP Form Validation

Hello everyone! I just created a form with Bootstrap framework via a tutorial that used PHP so send the form. I placate PHP in the html file and then saved as PHP and uploaded to my site. All is looking good and the modal is working properly however, the validation and mail submission isn’t. Any suggestions?

[php]<?php
if ($_POST[“submit”]) {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$message = $_POST[‘message’];
$human = intval($_POST[‘human’]);
$from = ‘Contact Form’;
$to = ‘REMOVED’;
$subject = ‘Message from Contact’;

	$body ="From: $name\n E-Mail: $email\n Message:\n $message";

	// Check if name has been entered
	if (!$_POST['name']) {
		$errName = 'Please enter your name';
	}
	
	// Check if email has been entered and is valid
	if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
		$errEmail = 'Please enter a valid email address';
	}
	
	//Check if message has been entered
	if (!$_POST['message']) {
		$errMessage = 'Please enter your message';
	}
	//Check if simple anti-bot test is correct
	if ($human !== 5) {
		$errHuman = 'Your anti-spam is incorrect';
	}

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result=‘

Thank You! I will be in touch
’;
} else {
$result=‘
Sorry there was an error sending your message. Please try again later.
’;
}
}
}
?>
[/php]

And the HTML

[code]

						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputName">Name</label>
								<div class="col-sm-10">
									<input class="form-control" id="inputName" name="name" placeholder="Name" type="text" value="<?php echo htmlspecialchars($_POST['name']); ?>">
										<?php echo "<p class='text-danger'>$errName</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputEmail">Email</label>
								<div class="col-sm-10">
									<input class="form-control" id="inputEmail" name="email" placeholder="[email protected]" type="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
										<?php echo "<p class='text-danger'>$errEmail</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputMessage">Message</label>
								<div class="col-sm-10">
									<textarea class="form-control" id="inputMessage" name="message" placeholder="What's On Your Mind?" rows="4"><?php echo htmlspecialchars($_POST['message']);?></textarea>
										<?php echo "<p class='text-danger'>$errMessage</p>";?>
								</div><!-- end col-sm-2 -->
						</div><!-- end form-group -->

						<div class="form-group">
							<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
								<div class="col-sm-10">
									<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
										<?php echo "<p class='text-danger'>$errHuman</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->		
												
						<div class="form-group">
							<div class="col-sm-12">
								<input id="submit" name="submit" type="submit" value="Send" class="btn btn-contact pull-right">
        					</div><!-- end col-sm-12 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<div class="col-sm-10 col-sm-offset-2">
								<?php echo $result; ?>
							</div><!-- end col-sm offset-2 -->
						</div><!-- end form-group -->
						
					</form><!-- end form -->

[/code]

Have you checked your junk folder to see if the mail ended up there? Check PHPMailer. They have their own tutorials and is a better system that using the default mail() function.

What is wrong with the validation? “It’s not working” does not give enough information for us to help you.

Astoncipher

Thanks for the quick reply and my apologies for not being clear.

  1. There is nothing in my SPAM folder.
  2. According to the tutorial that I used to design and write the code, if the form is not filled out properly, say if the user left out information or did not enter a valid email address, there should be a line of text under each field asking the user to fill out the form properly. It does not do this if I leave fields blank, the modal I have it in goes away. Also even if I enter proper information into the filed and submit it - no email is sent.

Thank you again for your help. I will checkout PHPMailer while I wait for your reply.

First, do you have error reporting turned on for the development?

Second, Post the code exactly as you have it. When I copied the parts into a php page I have numerous errors and warnings because of undefined variables.

And this is what I am using to get it:

[php]

<?php if ($_POST["submit"]) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $human = intval($_POST['human']); $from = 'Contact Form'; $to = 'REMOVED'; $subject = 'Message from Contact'; $body = "From: $name\n E-Mail: $email\n Message:\n $message"; // Check if name has been entered if (! $_POST['name']) { $errName = 'Please enter your name'; } // Check if email has been entered and is valid if (! $_POST['email'] || ! filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $errEmail = 'Please enter a valid email address'; } // Check if message has been entered if (! $_POST['message']) { $errMessage = 'Please enter your message'; } // Check if simple anti-bot test is correct if ($human !== 5) { $errHuman = 'Your anti-spam is incorrect'; } // If there are no errors, send the email if (! $errName && ! $errEmail && ! $errMessage && ! $errHuman) { if (mail($to, $subject, $body, $from)) { $result = '
Thank You! I will be in touch
'; } else { $result = '
Sorry there was an error sending your message. Please try again later.
'; } } } ?>
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputName">Name</label>
								<div class="col-sm-10">
									<input class="form-control" id="inputName" name="name" placeholder="Name" type="text" value="<?php echo htmlspecialchars($_POST['name']); ?>">
										<?php echo "<p class='text-danger'>$errName</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputEmail">Email</label>
								<div class="col-sm-10">
									<input class="form-control" id="inputEmail" name="email" placeholder="[email protected]" type="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
										<?php echo "<p class='text-danger'>$errEmail</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputMessage">Message</label>
								<div class="col-sm-10">
									<textarea class="form-control" id="inputMessage" name="message" placeholder="What's On Your Mind?" rows="4"><?php echo htmlspecialchars($_POST['message']);?></textarea>
										<?php echo "<p class='text-danger'>$errMessage</p>";?>
								</div><!-- end col-sm-2 -->
						</div><!-- end form-group -->

						<div class="form-group">
							<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
								<div class="col-sm-10">
									<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
										<?php echo "<p class='text-danger'>$errHuman</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->		
												
						<div class="form-group">
							<div class="col-sm-12">
								<input id="submit" name="submit" type="submit" value="Send" class="btn btn-contact pull-right">
        					</div><!-- end col-sm-12 -->
 						</div><!-- end form-group -->
 												
 						<div class="form-group">
							<div class="col-sm-10 col-sm-offset-2">
								<?php echo $result; ?>
							</div><!-- end col-sm offset-2 -->
						</div><!-- end form-group -->
						
					</form><!-- end form -->

[/php]

With one distinction, I have a method variable to post the information as opposed to not naming it, which defaults to $_GET not $_POST


I made some tweaks, see if you have mail either in the junk folder or the inbox from your form.

I just turned the error reporting on and I do see those warnings about the undefined variables as well. That portion go the PHP was meant to keep the text in the fields if there was an error, and then to show the user where the error was using the red warning underneath the field.

Here is where I got the tutorial and the source code from : http://bootstrapbay.com/blog/working-bootstrap-contact-form/

and my code :

[php]

<?php ini_set('display_errors',1); error_reporting(E_ALL); if ($_POST["submit"]) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $human = intval($_POST['human']); $from = 'Contact Form'; $to = 'REMOVED'; $subject = 'Message from Contact'; $body ="From: $name\n E-Mail: $email\n Message:\n $message"; // Check if name has been entered if (!$_POST['name']) { $errName = 'Please enter your name'; } // Check if email has been entered and is valid if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $errEmail = 'Please enter a valid email address'; } //Check if message has been entered if (!$_POST['message']) { $errMessage = 'Please enter your message'; } //Check if simple anti-bot test is correct if ($human !== 5) { $errHuman = 'Your anti-spam is incorrect'; } // If there are no errors, send the email if (!$errName && !$errEmail && !$errMessage && !$errHuman) { if (mail ($to, $subject, $body, $from)) { $result='
Thank You! I will be in touch
'; } else { $result='
Sorry there was an error sending your message. Please try again later.
'; } } } ?>
Name
<?php echo "

$errName

";?>
Email
<?php echo "

$errEmail

";?>
Message
<?php echo htmlspecialchars($_POST['message']);?> <?php echo "

$errMessage

";?>
2 + 3 = ?
<?php echo "

$errHuman

";?>
<?php echo $result; ?>

[/php]

I did receive you mail in the correct location yes!!

Not how I would have done it, and I would still encourage you to look into the PHP Mailer, it is far more robust.

Review the changes and see if you can explain the differences. Learn from this as opposed to copy and pasting it:

[php] <?php
if (isset($_POST[‘submit’])) {

$name = $_POST['name'];

$email = $_POST['email'];

$message = $_POST['message'];

$human = intval($_POST['human']);

$from = "FROM: {$_POST['email']}";

$to = 'REMOVED';

$subject = 'Message from Contact';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

// Check if name has been entered

if (! $_POST['name']) {
    
    $errName = 'Please enter your name';
}

// Check if email has been entered and is valid

if (! $_POST['email'] || ! filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    
    $errEmail = 'Please enter a valid email address';
}

// Check if message has been entered

if (! $_POST['message']) {
    
    $errMessage = 'Please enter your message';
}

// Check if simple anti-bot test is correct

if ($human !== 5) {
    
    $errHuman = 'Your anti-spam is incorrect';
}

// If there are no errors, send the email

if (! $errName && ! $errEmail && ! $errMessage && ! $errHuman) {
    
    if (mail($to, $subject, $body, $from)) {
        
        $result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        
        $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}

}
?>

						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputName">Name</label>
								<div class="col-sm-10">
									<input class="form-control" id="inputName" name="name" placeholder="Name" type="text" value="<?php if ( isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>">
										<?php if ( isset($errName)) echo "<p class='text-danger'>$errName</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputEmail">Email</label>
								<div class="col-sm-10">
									<input class="form-control" id="inputEmail" name="email" placeholder="[email protected]" type="email" value="<?php if ( isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>">
										<?php if ( isset($errEmail)) echo "<p class='text-danger'>$errEmail</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->
												
						<div class="form-group">
							<label class="col-sm-2 control-label" for="inputMessage">Message</label>
								<div class="col-sm-10">
									<textarea class="form-control" id="inputMessage" name="message" placeholder="What's On Your Mind?" rows="4"><?php if ( isset($_POST['message'])) echo htmlspecialchars($_POST['message']);?></textarea>
										<?php if ( isset($errMessage)) echo "<p class='text-danger'>$errMessage</p>";?>
								</div><!-- end col-sm-2 -->
						</div><!-- end form-group -->

						<div class="form-group">
							<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
								<div class="col-sm-10">
									<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
										<?php if ( isset($errHuman))echo "<p class='text-danger'>$errHuman</p>";?>
								</div><!-- end col-sm-10 -->
						</div><!-- end form-group -->		
												
						<div class="form-group">
							<div class="col-sm-12">
								<input id="submit" name="submit" type="submit" value="Send" class="btn btn-contact pull-right">
        					</div><!-- end col-sm-12 -->
 						</div><!-- end form-group -->
 												
 						<div class="form-group">
							<div class="col-sm-10 col-sm-offset-2">
								<?php if ( isset($result)) echo $result; ?>
							</div><!-- end col-sm offset-2 -->
						</div><!-- end form-group -->
						
					</form><!-- end form -->

[/php]

Thank you so much! I will definitely look into PHP Mailer! I have been reviewing this all day and my brain is fried! Will attempt PHPMailer when I am fresh and not drained!

Thank you again!

Sponsor our Newsletter | Privacy Policy | Terms of Service