PHP Mail form feedback

I have an HTML mail form that posts to a PHP file, then returns a new html “confirmation page”. I want the feedback to post in the original html page just below the send button, not call a separate file. like in a span or div tag. Here is my PHP code:

[php]

<?php if(isset($_POST['submit'])) { $to = "[email protected]"; $subject = "From your Site!"; // data the visitor provided $name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING); $email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL); $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING); //constructing the message $body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment"; // ...and away we go! mail($to, $subject, $body); // redirect to confirmation header('Location: confirmation.htm'); } else { // handle the error somehow } ?>

[/php]

And the HTML:

<form action="mailer.php" method="POST">
		<div class="span-12 form-row">
			<span>Name:</span><input name="name" type="text">
		</div>
		<div class="span-12 form-row  clear">
			<span>Email:</span><input name="email" type="text">
		</div>
		<div class="span-12 form-row clear">
			<span>Message</span><textarea name="comment"></textarea>
		</div>
		<div class="span-6 prepend-2">
			<input name="submit" type="submit" value="Send"><span class="form-status">Please complete all fields<span>
		</div>
		</form>

It is the (span class=“form-status”) where I would like the feedback to post.

Yes, I am new to PHP. Any help is greatly appreciated!
Thanks!

You could add a $_GET variable to your header redirect:

[php]
header(‘Location: confirmation.htm?status=sent’);[/php]

Then in confirmation.htm (needs to be confirmation.php for this to work):

[php]

if (isset($_GET[‘status’]) && ($_GET[‘status’] == ‘sent’)) {
echo ‘Like feedback and stuff’;
}
[/php]

Actually… you should expand on the above example and at least do a little error checking on mail():

[php]
$status = mail($to, $subject, $body);

if ($status !== FALSE) {
header(‘Location: confirmation.htm?status=sent’);
} else {
header(‘Location: confirmation.htm?status=error’);
}

//confirmation.php
if (isset($_GET[‘status’])) {

if ($_GET['status'] == 'sent')) {
    echo '<span class="form-status">Like feedback and stuff</span>';
} else if ($_GET['status'] == 'error') {
     echo '<span class="form-status">OMG! Panic and stuff!</span>';
} else {
    // not the prettiest code, but it's just for example's sake
}

}
[/php]

Does any of that help?

g0dzuki99,

Thank you but I am trying to have the feedback echo in the same page (contact.htm) as the form, not a “confirmation.php” file.

Apologies for leaving the Location in my example and not explaining it better.

No problem - concept is still the same though.

The code you posted is checking if the form was submitted and if not do something else. I think you’re pretty close… Instead we just need to check that the form was submitted and regardless, show the form and a status if its set.

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

$to = "[email protected]";
$subject = “From your Site!”;

// data the visitor provided
$name_field = filter_var($_POST[‘name’], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST[‘email’], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST[‘comment’], FILTER_SANITIZE_STRING);

// Error check fields

if (empty($name_field) || (empty($email_field) || (empty($comment)) {
$status = ‘Please complete all fields’;
}

//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";

// …and away we go!
$check = mail($to, $subject, $body);

if ($check !== FALSE) {
$status = ‘Mail sent successfully’;
}

// redirect to confirmation
//header(‘Location: confirmation.htm’); - commented out as you said you don’t want a separate confirmation page

}

echo '<form action="mailer.php" method="POST">
	<div class="span-12 form-row">
		<span>Name:</span><input name="name" type="text">
	</div>
	<div class="span-12 form-row  clear">
		<span>Email:</span><input name="email" type="text">
	</div>
	<div class="span-12 form-row clear">
		<span>Message</span><textarea name="comment"></textarea>
	</div>
	<div class="span-6 prepend-2">
		<input name="submit" type="submit" value="Send">';
           
              if (isset($status)) {
                    echo '<span class="form-status" style="color: red;">' . $status . '<span>';
              }
	
   echo '
           </div>
	</form>';

}
?> [/php]

Added error checking for $_POST fields and mail() return value. Removed the “else”. Only show the error/status if it has a value.

Is that closer to what you are looking for?

This is excellent! I see what this is doing.

My form page is “contact.htm” (where the echo will post) and I see here you have (form action=“mailer.php” method=“POST”)

I tried both but get this error for each:
Parse error: syntax error, unexpected ‘{’ in /htdocs/www/sandbox02/mailer.php on line 14

Line 14:
[php]if (empty($name_field) || (empty($email_field) || (empty($comment)) {[/php]

Oops… code before coffee error :stuck_out_tongue:

[php]if (empty($name_field) || (empty($email_field)) || (empty($comment))) {

}[/php]

the ( and )s were messed up…

Sponsor our Newsletter | Privacy Policy | Terms of Service