Mar*_*nce 6 php forms ajax jquery
当我通过Ajax向PHP脚本提交联系表单时遇到问题.我以前使用过jQuery/Ajax函数以及联系脚本,但是在这个问题上,进程运行到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);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
您可以在http://www.stillframe.com.au/test.php上看到正在运行的页面,您可以在上面的同一链接中看到没有页面媒体的表单但是test2.php
我还将没有任何其他jQuery的脚本添加到同一个url,但是test3.php直接发布到联系人PHP脚本以确认该脚本中没有错误.
已解决:从头部删除基本href标签,脚本现在工作正常.
不使用逐字段,而是使用FORM 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);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
小智 -4
由于以下原因,最好将 ajax 与原始 javascript 一起使用。
您的目标只是将变量从 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);
Run Code Online (Sandbox Code Playgroud)
}
在参数中相应地传递所有变量并创建一个 div#msg 来显示 php 的回复。
如果您有任何疑问,请访问http://www.thetutlage.com询问我
| 归档时间: |
|
| 查看次数: |
3702 次 |
| 最近记录: |