Php jqueryajax表单错误。空字符串
当我通过Ajax向PHP脚本提交联系人表单时,我遇到了一个问题。我以前使用过jQuery/Ajax函数以及contact脚本,但是在这个occaison上,进程运行到Ajax请求,然后返回错误:空字符串Php jqueryajax表单错误。空字符串,php,jquery,ajax,forms,Php,Jquery,Ajax,Forms,当我通过Ajax向PHP脚本提交联系人表单时,我遇到了一个问题。我以前使用过jQuery/Ajax函数以及contact脚本,但是在这个occaison上,进程运行到Ajax请求,然后返回错误:空字符串 $(document).ready(function(){ jQuery("#sendmail").click(function(){ var name = $("#name").val(); var phone = $("#phone").val();
$(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);
}
});
});
});
$(文档).ready(函数(){
jQuery(“#sendmail”)。单击(函数(){
var name=$(“#name”).val();
var phone=$(“#phone”).val();
var email=$(“#email”).val();
var text=$(“#消息”).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(“感谢您联系Stillframe Photography。在发送您的查询时,请稍等片刻。”;
美元(“#表格部门”).fadeIn(“慢”);
警报(datastr);
jQuery.ajax({
类型:“POST”,
url:“contact.script.php”,
数据:datastr,
成功:函数(html){
美元(“#响应”).fadeIn(“慢”);
$(“#响应”).html(html);
//setTimeout(“$”(“#响应”).fadeOut(“slow”)”,2000年);
}
,错误:函数(XMLHttpRequest、textStatus、error抛出){
$(“#response”).html('有一个错误');
log(“抛出错误”);
console.log(错误抛出);
}
});
});
});
您可以在上看到正在运行的页面,也可以在上面相同的链接上看到没有页面媒体的表单,但test2.php除外
我还向同一url添加了没有任何其他jQuery的脚本,但test3.php直接发布到contact php脚本,以确认该脚本中没有错误
已解决:从头部删除了base href标记,脚本现在可以正常工作。由于以下原因,最好将ajax与原始javascript一起使用
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);
}如果您有任何疑问,请让我回到上,而不是逐个字段使用,请使用表单ID
并序列化它。试试这个:
$(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);
}
});
});
});
$(文档).ready(函数(){
jQuery(“#sendmail”)。单击(函数(){
jQuery.ajax({
类型:“POST”,
url:“contact.script.php”,
数据:$('#myform')。序列化(),
cache:false,
成功:功能(输出){
$(“#form div”).css(“float”,“left”);
$(“#form div”).html(“感谢您联系Stillframe Photography。在发送您的查询时,请稍等片刻。”;
美元(“#表格部门”).fadeIn(“慢”);
$(“#响应”).html(输出);
美元(“#响应”).fadeIn(“慢”);
//setTimeout(“$”(“#响应”).fadeOut(“slow”)”,2000年);
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
$(“#response”).html('有一个错误');
log(“抛出错误”);
console.log(错误抛出);
}
});
});
});
您是否尝试过让您的URL包含数据,并删除“数据”行?url:'contact.script.php?name='+name+'&phone='+phone+'&email='+email+'&text='+text,我在test2.php上没有收到错误,它似乎在工作?谢谢James。在测试2中,你收到了“谢谢联系”的回复还是“有错误”的回复?这让我很困惑。我已将URL参数更改为Johnny在上面更新的URL参数。同样的回答。然而,当我收到发送给我的电子邮件时,似乎有些人一直在尝试该表单,从而表明该表单正在按预期工作。当我尝试FF和Chrome时,它不起作用,抛出错误并且没有发送电子邮件。只是为了澄清一下test3.php会起作用,因为它绕过Ajax请求并直接发布到php脚本。感谢您的帮助。此脚本生成了许多异常错误。-1。你会“永远”以错误结束吗?真正地我相信我能想出一个办法。你的答案是纯粹的偏好,它不能以OP编程的方式解决OP的问题。