2011-08-25 113 views
6

我在通过Ajax向PHP脚本提交联系表单时出现问题。我之前使用过jQuery/Ajax函数以及联系人脚本,但是在这个时间点上,进程运行得和ajax请求一样远,然后返回错误:空字符串。jQuery AJAX表单错误。空字符串

$(document).ready(function(){ 
    jQuery("#sendmail").click(function(){ 
     var name = $("#name").val(); 
     var phone = $("#phone").val(); 
     var email = $("#email").val(); 
     var text = $("#message").val(); 

     //var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text; 
     var datastr ='name=' + name + '&phone=' + phone + '&email=' + email + '&text=' + text; 
     $("#form-div").css("float", "left"); 
     $("#form-div").html("<p>Thank you for contacting Stillframe Photography.</p><p>Please wait for just a few moments while your enquiry is being sent.</p>"); 
     $("#form-div").fadeIn("slow"); 

     alert(datastr); 

     jQuery.ajax({ 
      type: "POST", 
      url: "contact.script.php", 
      data: datastr, 
      success: function(html){ 
      $("#response").fadeIn("slow"); 
      $("#response").html(html); 
      //setTimeout('$("#response").fadeOut("slow")',2000); 
      } 
      ,error: function (XMLHttpRequest, textStatus, errorThrown) { 
           $("#response").html('there was an error'); 
          console.log('error thrown'); 
          console.log(errorThrown); 
      } 
     }); 
    }); 
}); 

您可以在http://www.stillframe.com.au/test.php看到运行中的页面,你可以在看的形式,而不页面媒体以上述同样的链接,但test2.php

我还添加脚本没有任何其他jQuery到相同的url,但test3.php直接发布到联系人PHP脚本以确认该脚本中没有错误。

已解决:从头部删除了基本href标记,脚本现在可以正常工作。

+0

你试过让你的URL包含数据,并删除'数据'行吗? url:'contact.script.php?name ='+ name +'&phone ='+ phone +'&email ='+ email +'&text ='+ text, –

+0

我没有在test2.php上看到错误在工作吗? –

+0

谢谢詹姆斯。在测试2中,您是否收到“感谢您与我联系”的回复,或者您收到了“有错误”的回复? –

回答

-4

由于以下原因,最好在原始javascript中使用ajax。

  1. 代码非常简单和简短。
  2. 你永远不会以任何错误结束。
  3. 你的目标只是发送变量从JavaScript到PHP在正确的休息将被照顾,请为你添加一个示例代码。

    function insert(){ 
    if(window.XMLHttpRequest) 
    { 
        xmlhttp = new window.XMLHttpRequest(); 
    } 
    else 
    { 
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    } 
    
    xmlhttp.onreadystatechange = function() { 
        if(xmlhttp.readyState == '4' && xmlhttp.status == '200') 
        { 
         document.getElementById('msg').innerHTML = xmlhttp.responseText;  
        } 
    } 
    
    name = document.getElementById('insert').value; 
    
    parameters = 'insert=' + name; 
    
    xmlhttp.open('POST', 'day3.include.php', true); 
    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
    xmlhttp.send(parameters); 
    

    }

在参数传递所有变量的相应和创建一个div#味精显示从PHP的答复。

如果您有任何困惑问我回http://www.thetutlage.com

+5

-1。你将永远不会以错误结束?真?我确信我可以找出一个办法。你的回答是纯粹的偏好,它不能以他编程的方式解决OP的问题。 – bpeterson76

8

而不是使用逐个字段中,用表格ID <form id="myform">和序列化。试试这个:

$(document).ready(function(){ 
    jQuery("#sendmail").click(function(){ 
     jQuery.ajax({ 
      type: "POST", 
      url: "contact.script.php", 
      data: $('#myform').serialize(), 
      cache: false, 
      success: function(output) { 
       $("#form-div").css("float", "left"); 
       $("#form-div").html("<p>Thank you for contacting Stillframe Photography.</p><p>Please wait for just a few moments while your enquiry is being sent.</p>"); 
       $("#form-div").fadeIn("slow"); 
       $("#response").html(output); 
       $("#response").fadeIn("slow"); 
       //setTimeout('$("#response").fadeOut("slow")',2000); 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
           $("#response").html('there was an error'); 
          console.log('error thrown'); 
          console.log(errorThrown); 
      } 
     }); 
    }); 
}); 
+0

感谢您的帮助。该脚本生成了大量异常错误。 –

+1

什么样的错误?他们是什么? –