Page 1 of 1

Help! After submit, jumps to .php file

Posted: Tue Sep 08, 2015 7:45 pm
by xxshadowxx
Hi All,

I have this problem. When I fill in all the fields in contact form, on clicking the "Submit" button, it jumps to .php file link on server and I did not receive the message from contact form. I have been stuck for days and I need to hand in the assignment tomorrow. Could anyone be kind enough to walk-through with me which or where of my codings went wrong.

Below is the HTML and PHP coding.

HTML:

Code: Select all

<div class="container">
		<div class="row">
			<div class="col-lg-12 col-lg-offset-0">
				<div class="col-md-12 center-block">

                <h5 class="auto-style1">Contact Form</h5>
                	<p class="required small" style="color: #FF0000; font-size: 90%">
					<strong>* = Required fields</strong></p>
<!--begin HTML Form-->
<form class="form-horizontal" role="form" method="post" action="sendemail.php">

	<div class="form-group">
		<label for="name" class="col-sm-3 control-label"><span class="required">*</span> Name:</label>
	<div class="col-sm-9">
		<input type="text" class="form-control" id="name" name="name" placeholder="First & Last">
	</div>
</div>

	<div class="form-group">
		<label for="email" class="col-sm-3 control-label"><span class="required">*</span> Email: </label>
	<div class="col-sm-9">
		<input type="email" class="form-control" id="email" name="email" placeholder="example@example.com">
	</div>
</div>

	<div class="form-group">
		<label for="phone" class="col-sm-3 control-label"><span class="required">*</span> Phone: </label>
	<div class="col-sm-9">
		<input type="tel" class="form-control" id="phone" name="phone" placeholder="(65) 6123-4567">
	</div>
</div>

	<div class="form-group">
		<label for="message" class="col-sm-3 control-label"><span class="required">*</span> Message:</label>
	<div class="col-sm-9">
		<textarea class="form-control" row="4" name="message" placeholder="Your Message"></textarea>
	</div>
</div>

	<div class="form-group">
		<label for="human" class="col-sm-3 control-label"><span class="required">*</span> Human Test:</label>
	<div class="col-sm-9">
		<h5 class="human">4 + 4 = ?</h5>
			<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
	</div>
</div><br>

	<div class="form-group">
		<div class="col-sm-offset-3 col-sm-6 col-sm-offset-6">
			<button type="submit" id="submit" name="submit" class="btn-lg btn-primary btn-block pull-right">SUBMIT</button>
	</div>
</div>
PHP:

Code: Select all

<?php
	$result = $name = $email = $phone = $message = $human = "";
	$errName = $errEmail = $errPhone = $errMessage = $errHuman = "";
		if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
}
	$from = 'webmaster@gmail.com';
	$to = 'me@gmail.com';
	$subject = 'MESSAGE FROM WEB CONTACT FORM';
        $headers = "From:$from\r\nReply-to:$email";
        $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message: $message";
}

if (empty($_POST["name"])) {
$errName = "Please enter your name.";
} else {
    $name = test_input($_POST["name"]);
}

if (empty($_POST["email"])) {
$errEmail = "Please enter your email address.";
} else {
    $email = test_input($_POST["email"]);
    
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $errEmail = "Invalid email format.";
    }
}    

if (empty($_POST["phone"])) {
$errPhone = "Please enter your phone number";
} else {
    $phone = test_input($_POST["phone"]);
}

if (empty($_POST["message"])) {
$errMessage = "Please enter your message.";
} else {
    $message = test_input($_POST["message"]);
}

if (empty($_POST["human"])) {
$errHuman = "Please enter the sum.";
} else {
     if ($human !== 8 ) {
     $errHuman = 'Wrong answer. Please try again.';
        }
}

if (!$errName && !$errEmail && !$errPhone &&  !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success"><h2><span class="glyphicon glyphicon-ok"></span> Message sent!</h2><h3>Thank you for contacting us. Someone will be in touch with you soon.</h3></div>';
    } else {
        $result='<div class="alert alert-danger"><h2><span class="glyphicon glyphicon-warning-sign"></span> Sorry there was a form processing error.</h2> <h3>Please try again later.</h3></div>';
       }
    }
}

	function test_input($data) {
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
	$data = (filter_var($data, FILTER_SANITIZE_STRING));
	return $data;
}


?>
I would really be thankful if someone can walk-through with me where went wrong and why it jumps to .php file link instead of submitting and sending to my email inbox.

Yours Sincerely,
xxshadowxx

Re: Help! After submit, jumps to .php file

Posted: Tue Sep 08, 2015 7:51 pm
by requinix
You define a few error messages but don't output them anywhere. You have a $result with the message but don't output that either.

The HTML and PHP are in the same file, right? With the PHP code placed above the HTML? Now you need to modify the HTML (by adding some PHP) so that it will show the $result, and possibly the specific error messages too.

Re: Help! After submit, jumps to .php file

Posted: Tue Sep 08, 2015 9:52 pm
by Christopher
Is that the whole PHP file? It looks like it has errors. Is it failing and the errors are going to the logs?