Help with Contact Form info delivery

This contact Form I’m using needs a little expert guidance.

The Form looks fine, I believe:

<form action="../contact_form_handle.php" method="post" name="contact_us" onSubmit="return capCheck(this);">
........
</form>

But when I ‘submit’ I don’t see any information arrive. Here’s the ‘contact_form_handle’ php page code:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//Start Contact Us -
if($_POST['submited'] == "1"){

$your_email = "support@....com";  
$from = "From: contact-form@....com". "\r\n"; 
$user_email = $_POST['email'];
$user_name = $_POST['name'];
$subject = $_POST['subject'];
$email_body = $_POST['comments'];

if($user_email == "" or $user_name == "" or $subject == "" or $email_body == ""){
$error = "Please Complete All Required* Fields";
}else{

$message = "ContactUs : \n \nUsers Email : $user_email \nUsers Name : $user_name  \n\n\n Users Message : \n\n$email_body";

//SEND THE EMAIL -

mail($your_email, $subject, $message, $from);

$result = "Your Message Has Been Sent. Thank You";

}
}
$message = "ContactUs : \n \nUsers Email : $user_email \nUsers Name : $user_name \n\n\n Users Message : \n\n$email_body";
}
//End Contact Us -
?>

Any help will be appreciated.

What does that do?

That’s because you are still doing if($_POST['submit'] == "1"). You don’t need this anymore since you are checking to see if the form was submitted already by using the proper $_SERVER['REQUEST_METHOD']. And the blank page probably is the end of that if statment too because if you had put an else there, it will most likely go into that else statment. So if you are already using $_SERVER['REQUEST_METHOD'], you don’t need that legacy logic anymore.

2 Likes

Thanks for the help.It delivers the info successfully,
Would you be interested in letting me know what I’m missing to display
“Your Message Has Been Sent. Thank You” above the Form, once it’s been submitted?
I look forward to any input

You could put mail() in an if.

if (mail($your_email, $subject, $message, $from)) {
    $result = "Your Message Has Been Sent. Thank You";
}
else { $result = "There was an error sending Your Message"; }

This is not 100% reliable, mail() can fail to send but still return true. Needles to say, you will at some point echo $result.

You may want to consider more robust validation rather than just checking for empty fields. At least validate the email address.

Thanks for your replies.
Thanks works successfully making the message appear at …/contact_form_handle.php page.

Can someone direct me as to what is the best way to have the Form submit, but not leave the Form page, and have the Message appear above the Form, after submit, please?

What you are looking for is Jquery. When you click on the submit button, based on what you are using as a trigger. You can make it submit to a page and if the form was submitted correctly, you would then request what ever page you are using to display the message in a .html(); function in Jquery.

@spaceshiptrooper suggest jquery and using ajax would be a great option but if your not wanting to use ajax you can use cookies or a session variable to save the input data as well as return your error messages for your html to display. So how it would is at the end of your validation of your data you would then store all the sent fields and error messages or success save those in a cookie or session and a simple header location redirect and you will have the data of the previous request and your error messages or success messages.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.