event.preventDefault(); not working when submitting form

John Siniger

I am trying to put the result from a submitted form in to a div instead of opening it inside a new window. The problem is that my event.preventDefault(); doesn't seem to be working and I don't understand why. The result, after I hit the submit button is always to open the contact-form-handler.php, which is the script file.

Here is the code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

CONTACT FORM:

<form class="form-container" id="contactForm" method="post" action="contact-form-handler.php">
<div class="form-title">Full Name:</div>
<input class="form-field" type="text" name="name" /><br />
<div class="form-title">Email Address:</div>
<input class="form-field" type="text" name="email" /><br />
<div class="form-title">Phone Number:</div>
<input class="form-field" type="text" name="phone" /><br />
<div class="form-title">Message:</div>
<textarea class="form-field" rows="8" cols="34" name="message"></textarea><br />
<div id="contactResponse"></div>
<div class="submit-container">
<button type="submit" class="submit-button">Send</button>
</div>
</form>

SCRIPT CODE:

<script type="text/javascript">
$("#contactForm").submit(function(event) 
     {
         /* stop form from submitting normally */
         event.preventDefault();

         /* get some values from elements on the page: */
         var $form = $( this ),
             $submit = $form.find( 'button[type="submit"]' ),
             name_value = $form.find( 'input[name="name"]' ).val(),
             email_value = $form.find( 'input[name="email"]' ).val(),
             phone_value = $form.find( 'input[name="phone"]' ).val(),
             message_value = $form.find( 'textarea[name="message"]' ).val();


         /* Send the data using post */
         var posting = $.post( "contact-form-handler.php", { 
                           name: name_value, 
                           email: email_value, 
                           phone: phone_value, 
                           message: message_value 
                       });

         posting.done(function( data )
         {
             /* Put the results in a div */
             $( "#contactResponse" ).html(data);

             /* Change the button text. */
             $submit.text('Sent, Thank you');

             /* Disable the button. */
             $submit.attr("disabled", true);
         });
    });
</script>

PHP CODE:

<?php 
$myemail = '[email protected]';//<-----Put Your email address here.
$name = $_POST['name']; 
$phone = $_POST['phone'];
$email_address = $_POST['email']; 
$message = $_POST['message']; 


    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "Contact Form Emocool Website ".
    " Here are the details:\n Name: $name \n Telefono: $phone \n Email: $email_address \n Message \n $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    if(mail($to,$email_subject,$email_body,$headers))
    {
    echo "Thank you for contacting us";
    }
    //redirect to the 'thank you' page
    else
    {
    echo "Sorry, there has been an error";  
    }

?>

So the problem is when I hit the submit button, instead of the result showing inside the #contactResponse div, it is showing inside new page of the script contact-form-handler.php

Any suggestions why this is not working?

Harvey A. Ramer

My best guess is that you are adding your #contactForm submit listener before the form is actually rendered. Try wrapping your jQuery in $(document).ready(function () {});

I don't see any other issues.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

preventDefault on submitting form

Event.preventDefault() not working, form submitted twice

event.preventDefault not working for form created with JQuery

Loading Indicator is not working when submitting the form in reactjs

The event.preventDefault is not working

Why does preventDefault() of a click event also prevent my form from submitting?

Is there a way of submitting this form after using the preventDefault function?

How to trigger submitting form automatically and use preventDefault

event.preventDefault( ) is NOT working in React

event.preventdefault is not working in firefox

event.preventDefault(); not working in ajax

event.preventDefault() not working on mozilla

ReactJS form submit preventdefault not working

preventDefault() not working for preventing submission of form

Form submitting when clicked

TypeError when submitting form

Submit a form after event.preventDefault (event.preventDefault behavior)

React form event.preventDefault() not working if handler calls setState on sibling component

Form submitting without refresh not working

Submitting a form via AJAX not working

stop form from submitting (tried e.preventDefault() after $.post but it stops form from submitting

How to submit a form after event.preventDefault

Reactjs default Form validation with event.preventDefault()

Can't submit form after event preventDefault

Form validation is not working in Ajax form submitting

anchor link not working with jquery event.preventDefault;

jquery .click() event.preventDefault() not working

event.preventDefault() function not working in IE

event preventDefault apparently not working properly in Meteor