2017-09-01 80 views
0

对不起,标题戈尔我有点在我的头上,并尝试了一切,我可以在网上找到。我试图通过邮件传递数据到我的sendjs.php文件,我有一个问题。Ajax成功响应无效。不通过IE11的数据

  1. ajax success函数不适用if语句。

住在这里的网站:www.diysoakwells.com.au(你可以添加一个项目和结帐测试)。

我甚至不知道从哪里开始诚实,所以任何信息将不胜感激,我将根据要求更新信息。

app.js

$(function() { 


    // Get the form. 
    var form = $("#ajax-contact"); 

    // Get the messages div. 
    var formMessages = $("#form-messages"); 

    var spinner = $("#spinner"); 

    var submit = $("#submit"); 

    // Set up an event listener for the contact form. 
    $(form).submit(function(e) { 
     // Stop the browser from submitting the form. 
     e.preventDefault(); 

     //display the cog animation 
     $(spinner).removeClass("hidden"); 
     //hide the submit button 
     $(submit).addClass("hidden"); 



     jsonObj=[]; 
     for(i=1;i<$(".item-price").length;i++) 
     { 
      var items={}; 
      var itemname = $(".item-name").get(i); 
      var itemprice = $(".item-price").get(i); 
      var itemquantity = $(".item-quantity").get(i); 
      var itemtotal = $(".item-total").get(i); 
      items["name"] = itemname.innerHTML; 
      items["price"] = itemprice.innerHTML; 
      items["quantity"] = itemquantity.innerHTML; 
      items["total"] = itemtotal.innerHTML; 
      jsonObj.push(items); 

     } 

     console.log(items); 

     var formdata = {}; 
     formdata["textbox"] = $("#textbox").val(); 
     formdata["name"] = $("#name").val(); 
     formdata["phone"] = $("#phone").val(); 
     formdata["email"] = $("#email").val(); 
     formdata["address"] = $("#address").val(); 
     formdata["grandtotal"] = simpleCart.grandTotal(); 
     var x = 
     { 
      "cart" : jsonObj, 
      "formdata" : formdata, 
      "captchaResponse" : $("#g-recaptcha-response").val() 
     }; 
     //jsonString = jsonObj+formdata; 
     var y = JSON.stringify(x); 
     console.log(y); 
     var result = jQuery.parseJSON(y); 
     console.log(result); 


     // Serialize the form data. 
     //var formData = $(form).serialize(); 

     // Submit the form using AJAX. 
     $.ajax({ 
      type: "post", 
      url: "sendjs.php" , 
      //url: $(form).attr("action"), 
      data: y, 
      contentType: "application/json; charset=utf-8", 
      traditional: true, 
      success: function (response) { 
         if(response=="Thank You! Your message has been sent.") 
         { 
        //remove the button animation 
        $(spinner).addClass("hidden"); 
        $(formMessages).removeClass("error"); 
        $(formMessages).addClass("success"); 
        $("#textbox").val(""); 
        $("#name").val(""); 
        $("#email").val(""); 
        $("#message").val(""); 
        $("#phone").val(""); 
        $("#address").val(""); 

         } 
         else 
         { 
        $(formMessages).removeClass("success"); 
        $(formMessages).addClass("error"); 
        $(spinner).addClass("hidden"); 
        $(submit).removeClass("hidden"); 
         } 

       $(formMessages).text(response); 

      } 
     }); 
    }); 

}); 

sendjs.php

<?php 
//Debugging 
//ini_set('display_errors', 1); 
//error_reporting(E_ALL); 

//replaces file_get_contents due to restrictions on server 
function get_data($url) 
    { 
     $ch = curl_init(); 
     $timeout = 5; 
     curl_setopt($ch,CURLOPT_URL,$url); 
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
     curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
     $data = curl_exec($ch); 
     curl_close($ch); 
     return $data; 
    } 

    //turn url_fopen on due to restrictions on server 
    //ini_set('allow_url_fopen', true); 

    date_default_timezone_set('Australia/Perth'); 
    $time = date ("h:i A"); 
    $date = date ("l, F jS, Y"); 
    $json = file_get_contents('php://input'); 
    $obj = json_decode($json,true); 
    $captcha = $obj["captchaResponse"]; 
    $captcha; 
    $secretKey = "scrubbed"; 
    $ip = $_SERVER['REMOTE_ADDR']; 
    $response = get_data("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); 

    //not used due to server restictions 
    //$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); 

    $responseKeys = json_decode($response,true); 

    if(intval($responseKeys["success"]) !== 1) { 
     echo "Please Click on the Captcha"; 
     return false; 
    } 
    else 
    { 
    //echo $items; 
    $name = $obj["formdata"]["name"]; 
    $phone = $obj["formdata"]["phone"]; 
    $email = $obj["formdata"]["email"]; 
    $textbox = $obj["formdata"]["textbox"]; 
    $address = $obj["formdata"]["address"]; 
    $grandtotal = $obj["formdata"]["grandtotal"]; 
    $text = "<html style='font-family:arial'> 
    <body> 
     <h1 style='color:crimson;'>DIY Soakwells</h1> 
     <p>This order was submitted from www.diysoakwells.com.au on $date at $time</p> 
     <p>$name thank you for your order inquiry. Deliveries are normally every Friday, we will be in contact shortly to discuss your order and confirm a time.</p> 
     <p>An invoice will be issued after delivery for payment via bank transfer.</p> 
     <p>In the meantime if you haven't already seen it, you can take a look at www.soakwellcalculator.com.au to confirm the number of soakwells you ordered will be adequate.</p> 
     <br> 

     <h2 style='color:crimson;'>CUSTOMER DETAILS</h2> 
     <p><b>Email:</b>\n$email</p> 
     <p><b>Name:</b>\n$name</p> 
     <p><b>Phone:</b>\n$phone</p> 
     <p><b>Delivery Address:</b>\n$address</p> 
     <p><b>Message:</b>\n$textbox</p> 
     <br> 

     <h2 style='color:crimson;'>ORDER DETAILS</h2>"; 

     $items_in_cart = count($obj["cart"]); 
     for($i=0; $i < $items_in_cart; $i++) { 
      $iname = $obj["cart"][$i]["name"]; 
      $price = $obj["cart"][$i]["price"]; 
      $quantity = $obj["cart"][$i]["quantity"]; 
      $total = $obj["cart"][$i]["total"]; 
      //display looped cart data  
      $items .= "<p>$iname x $quantity - $price <small>ea.</small> <b>Sub Total: </b> $total .</p>"; 
     } 

     $final_total ="<br> 
     <p><b>Total: </b>$$grandtotal <small>inc. GST</small></p> 
     </body> 
     </html>"; 

     //Email Content 
     $body = $text.$items.$final_total; 

     // Set the email subject. 
     $subject = "New order from $name"; 

     // Build the email content. 
     $email_content = $body; 

     // Build the email headers. 
     $email_headers = 'MIME-Version: 1.0' . PHP_EOL; 
     $email_headers .= 'Content-Type: text/html; charset=ISO-8859-1' . PHP_EOL; 
     //$email_headers .= 'To:' . $name . '<' . $email . '>' . PHP_EOL; 
     $email_headers .= 'From: DIY Soakwells <[email protected]>' . PHP_EOL; 
     $email_headers .= 'CC: [email protected]' . PHP_EOL; 
     $email_headers .= 'Reply-To: DIY Soakwells <[email protected]>' . PHP_EOL; 
     $email_headers .= 'Return-Path: DIY Soakwells <[email protected]>' . PHP_EOL; 
     $email_headers .= 'X-Sender: DIY Soakwells <[email protected]' . PHP_EOL; 
     $email_headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL; 
     //$email_headers .= 'X-Priority: 1' . PHP_EOL; 



     //validate Email 
     $email_check = filter_input(INPUT_POST, $email, FILTER_VALIDATE_EMAIL); 
     //Recipients 
     $to = $email; 

     if (mail($to, $subject, $email_content, $email_headers, '[email protected]')) { 
      // Set a 200 (okay) response code. 
      //http_response_code(200); 
      echo "Thank You. Your order has been sent and a copy mailed to your inbox."; 
     } else { 
      // Set a 500 (internal server error) response code. 
      //http_response_code(500); 
      echo "There appears to be an issue with our server, please ring 0420 903 950 or email [email protected]"; 
     }  
    }  
?> 

希望有人可以给我一些提示。

+1

你是如何确定*“'jsonObj'未在IE11通过“*?您应该能够看到* Network *控制台中发布的数据。 – Phil

+1

此外,您不需要“传统”属性;这只适用于'application/x-www-form-urlencoded'数据。实际上,你应该设置'processData:false'而不是 – Phil

+0

谢谢phil。我将把问题分成两部分,并按照要求进行更新。 – JPB

回答

1

,因为你的条件是response == "Thank You! Your message has been sent."

和你的结果是 "Please Click on the Captcha", "Thank You. Your order has been sent and a copy mailed to your inbox.", "There appears to be an issue with our server, please ring 0420 903 950 or email [email protected]".

因此所有的结果都会做其他部分