Contact Me Page

I’ve got this script which is meant to send emails to me but what I want to do is instead of redirecting the user(s) to a different page to display the results I would like it to show the success message or fail message on the same page as the form. I hope I am making my self clear. I don’t know how to implement ajax.
index.html:

<!DOCTYPE html>

<html>
<head>
    <title></title>
</head>

<body>
    <form action="send_form_email.php" id="contactform" method="post" name=
    "contactform">
        <table width="450px">
            <tr>
                <td valign="top"><label for="title">Title *</label></td>

                <td valign="top"><input maxlength="50" name="title" size="30"
                type="text"></td>
            </tr>

            <tr>
                <td></td>
            </tr>

            <tr>
                <td valign="top"><label for="fname">First Name *</label></td>

                <td valign="top"><input maxlength="50" name="fname" size="30"
                type="text"></td>
            </tr>

            <tr>
                <td></td>
            </tr>

            <tr>
                <td valign="top"><label for="mname">Middle Name</label></td>

                <td valign="top"><input maxlength="50" name="mname" size="30"
                type="text"></td>
            </tr>

            <tr>
                <td valign="top"><label for="lname">Last Name *</label></td>

                <td valign="top"><input maxlength="50" name="lname" size="30"
                type="text"></td>
            </tr>

            <tr>
                <td valign="top"><label for="suffix">Suffix</label></td>

                <td valign="top"><input maxlength="80" name="suffix" size="30"
                type="text"></td>
            </tr>

            <tr>
                <td valign="top"><label for="message">Message *</label></td>

                <td valign="top">
                <textarea cols="25" maxlength="1000" name="message" rows="6">
</textarea></td>
            </tr>

            <tr>
                <td></td>
            </tr>

            <tr>
                <td valign="top"><label for="email">Email Address
                *</label></td>

                <td valign="top"><input maxlength="80" name="email" size="30"
                type="text"></td>
            </tr>

            <tr>
                <td colspan="2" style="text-align:center"><input type="submit"
                value="Submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

send_form_email.php:
[php]

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; $email_subject = "Your email subject line"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['title']) || !isset($_POST['fname']) || !isset($_POST['mname']) || !isset($_POST['lname']) || !isset($_POST['suffix']) || !isset($_POST['message']) || !isset($_POST['email'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $title_name = $_POST['title']; // required $first_name = $_POST['fname']; // required $middle_name = $_POST['mname']; $last_name = $_POST['lname']; // required $suffix_name = $_POST['suffix']; $message = $_POST['message']; // required $email_from = $_POST['email']; // required $error_message = ""; $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$title_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$middle_name)) { $error_message .= 'The Middle Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$suffix_name)) { $error_message .= 'The Suffix Name you entered does not appear to be valid.
'; } if(strlen($message) < 2) { $error_message .= 'The Message you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Title Name: ".clean_string($title_name)."\n"; $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Middle Name: ".clean_string($middle_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Last Name: ".clean_string($suffix_name)."\n"; $email_message .= "Comments: ".clean_string($message)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you very soon. <?php } ?>

[/php]

Well, first, let’s explain the pro’s and con’s to using the ajax way to submit your form.

The pro’s: It is simple to do, it is nice to have the PHP called from the page and keep the page in place.

Con’s: It opens up security issues where any user can just VIEW-SOURCE and see the AJAX code. This allows them to locate your PHP file and then, they can copy that PHP file and investigate your code. So, you must make sure to protect your files on the server so that they can not be download directly.

(AJAX is a sub-version of Javascript, so it is CLIENT-SIDE and visible to all. PHP is SERVER-SIDE and NOT visible to anyone but the owner.)

Now that said, here is a link that explains one way to handle this. The important part is the first AJAX section. It shows how to call the PHP file using a standard BUTTON instead of a SUBMIT button. The differences are small in those two buttons, but, it seems easier using their code. You change the name of the PHP file and whatever you want to pass to it. If you can not figure it all out, post what you have and we can help.

Good luck! http://stackoverflow.com/questions/11640598/send-data-to-php-with-ajax-using-formdata

Sponsor our Newsletter | Privacy Policy | Terms of Service