Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX/PHP form not sending information
    primarykey
    data
    text
    <p>Hey guys i have a form that's supposed to send a message without reloading the page. I've used this tutorial here: <a href="http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/" rel="nofollow">http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/</a> and adapted it to suit my needs. here is the php:</p> <pre><code>&lt;?php session_start(); // Define some constants define( "RECIPIENT_NAME", "John Smith" ); define( "RECIPIENT_EMAIL", "myemail@email.com" ); define( "EMAIL_SUBJECT", "Visitor Message" ); // Read the form values and define variables $success = false; $senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : ""; $senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : ""; $message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : ""; $honeypot = $_POST['url']; $code = $_POST['code']; //if the honeypot is filled out, dont send the form if (!empty($honeypot)){ $success = false; } // If all values exist and the code matches, send the email else if ( $senderName &amp;&amp; $senderEmail &amp;&amp; $message &amp;&amp; $code == $_SESSION['random_code'] ) { $recipient = RECIPIENT_NAME . " &lt;" . RECIPIENT_EMAIL . "&gt;"; $headers = "From: " . $senderName . " &lt;" . $senderEmail . "&gt;"; $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers ); } // Return an appropriate response to the browser if ( isset($_POST["ajax"]) ) { echo $success ? "success" : "error"; } else { ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Thanks!&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php if ( $success ) echo "&lt;p&gt;Thanks for sending your message! We'll get back to you shortly.&lt;/p&gt;" ?&gt; &lt;?php if ( !$success ) echo "&lt;p&gt;There was a problem sending your message. Please try again.&lt;/p&gt;" ?&gt; &lt;p&gt;Click your browser's Back button to return to the page.&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php } ?&gt; </code></pre> <p>and here is the javascript:</p> <pre><code> function submitForm() { var contactForm = $(this); //submit the form to the PHP script via Ajax $('#sendingMessage').fadeIn(); $.ajax( { url: contactForm.attr( 'action' ) + "?ajax=true", type: contactForm.attr( 'method' ), data: contactForm.serialize(), success: submitFinished } ); // Prevent the default form submission occurring return false; } function submitFinished( response ) { response = $.trim( response ); $('#sendingMessage').fadeOut(); if ( response == "success" ) { // Form submitted successfully: // 1. Display the success message // 2. Clear the form fields $('#successMessage').fadeIn().delay(5000).fadeOut(); $('#name').val( "" ); $('#email').val( "" ); $('#message').val( "" ); } else { // Form submission failed: Display the failure message, $('#failureMessage').fadeIn().delay(5000).fadeOut(); } } </code></pre> <p>that function is run when the user clicks the send button and after it's checked that all the fields have been filled out. I think it's tripping up down the end of the php where it says if(isset($_POST['ajax'])) because i keep getting redirected to the html fallback back, as if I dont have javascript enabled (which i do by the way). you can see a working example here: mattsandersdesign.com/test/contact2.html thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload