简体   繁体   中英

jquery Ajax doesn't work for post method for ie 8 or 9

I have this for document ready using jquery 1.11 from cdn and jquery migrate 1.2.1 from cdn. The ajax is to send form values to a file "mail.php" that sits in the same directory and simply checks if it was called via ajax then grabs the post variables and send them via email. It can't be a cross domain issue. Also, the alerts below confirm that it is grabbing the input values OK. This works fine for ie10+ and all other browsers.

However, for ie8+9 - the email is sent to me blank..in other words the post variables aren't being recieved?

  $("#contactform").submit( function (e) {
        e.returnValue=false;
        e.preventDefault();
        alert('form submitted');
        if (checkValidation()) {
            //if valid, send ajax
var name=$('#form-name').val();
var email=$('#form-email').val();
        var contact_number=$('#form-contact-number').val();
        var message=$('#form-message').val();
        alert(name+email+contact_number+message);
            $.ajax({
                type: 'POST',
                url: 'mail.php',
                //data: JSON.stringify(parameters),
               contentType:  "json",
               // data: $(this).serialize(),

data:{'name':name,'email':email,'contact_number':contact_number,'message':message},
                dataType: "text",
                cache: false,
                success: function(data) {
                    // do something with ajax data

                    $('.form-response').css({color:'black',backgroundColor:'white',textAlign:'center'}).text('Thank you, we will contact you shortly.').show();

                    $('input').val('').trigger('blur');
                    $('textarea').val('').trigger('blur');
        setTimeout(function(){
            $('.form-response').hide();
            scroll_to_top();

        },3000);

                },
                error:function (xhr, ajaxOptions, thrownError){
                    console.log('error...', xhr);
                    //error logging
                },
                complete: function(){
                    //afer ajax call is completed
                }
            });

        } else {
            alert('Please re-enter your input and try again.');
            $('input').val('');
            $('textarea').val('');
            $("input").trigger("blur");
            $("textarea").trigger("blur");
            $('#form-name').focus();

        }


    });

My form on index.html looks like:

<form class="form-style validate-form clearfix" id="contactform" action="mail.php" method="POST" role="form">
<div class="col-md-6"><div class="form-group"><input type="text" class="text-field form-control validate-field required" data-validation-type="string" id="form-name" placeholder="Full Name" name="name"></div>

<div class="form-group"><input type="email" class="text-field form-control validate-field required" data-validation-type="email" id="form-email" placeholder="Email Address" name="email"></div>
<div class="form-group"><input type="tel" class="text-field form-control validate-field phone" data-validation-type="phone" id="form-contact-number" placeholder="Contact Number" name="contact_number">
<input type="text" id="address-input" name="address" style="display: none!important;"></div></div><div class="col-md-6">
<div class="form-group"><textarea placeholder="Message..." id="form-message" class="form-control validate-field required" name="message"></textarea></div>
<div class="form-group"><button type="submit" id="submitBtn" class="btn btn-sm btn-outline-inverse">Submit</button></div></div>
</form>

my mail.php:

<?php
if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
{
       # is ajax

       if (empty($_POST["address"])) {
    $from = $_POST["name"]; // sender
    $subject = 'From: ' . $from;
    $email = $_POST["email"];
    $tel = $_POST["contact_number"];
    $message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
 $textToSend = 'From: ' . $from . "\n";
    $textToSend .= 'Email: ' . $email . "\n";
    $textToSend .= "Phone: " . $tel . "\n";
    $textToSend .= 'Message: ' . $message . "\n";
// send mail
    mail("contact@domain.net", $subject, $textToSend, "From: $from\n");
    echo "Thank you, we will contact you shortly.";


    echo '
<script>
$("input").val("");
$("textarea").val("");
setTimeout(function(){

scroll_to_top();

},3000);

</script>



';

} else {
    echo 'Thank you, we will contact you shortly.';

    echo '
<script>
$("input").val("");
$("textarea").val("");
setTimeout(function(){

scroll_to_top();

},3000);

</script>



';
}
}else{
     header( 'Location: http://www.myhomepage.net' ) ;  
       die();


       }

Edit:: incase a question about it gets brought up.. I use the address field in the form to act as my "honeypot" method .. which basically means if the field address ( which is set to display none ) is filled it..then the submission was most likely a bot and will be discarded.

可能是因为您的变量被双引号括起来,以了解更多信息https://stackoverflow.com/a/7081920/3501422

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM