[英]Ajax with submit form and php
我不确定我做错了什么。 我搜索了多个论坛,但没有找到解决方案。 我定义了以下 ajax 以从表单提交值。 我有警报 function 以确保正在调用此代码(确实如此),但未执行 process.php。 任何想法为什么? 这是 ajax 代码(我的 dataString 包含所有变量的值并且 $_POST 变量似乎没有问题。
// JavaScript Document
$(document).ready(function(){
$(".button").click(function() {
// validate and process form here
//alert("yayyy");
var company = $("input#company").val();
var jobfunc = $("input#jobfunc").val();
var location = $("input#location").val();
var overall = $("input#overall").val();
var detail = $("textarea#detail").val();
var pros = $("textarea#pros").val();
var cons = $("textarea#cons").val();
var sr_mgmt = $("textarea#sr_mgmt").val();
var submitted_by = $("input#submitted_by").val();
var classof = $("input#classof").val();
var school = $("input#school").val();
var anonymous = $("input#anonymous").val();
if (anonymous == 'on')
{
var anonymous_tmp = 'Y';
}
else
{
var anonymous_tmp = 'N';
}
var dataString =
'company=' + company +
'&jobfunc=' + jobfunc +
'&location=' + location +
'&overall=' + overall +
'&detail=' + detail +
'&pros=' + pros +
'&cons=' + cons +
'&sr_mgmt=' + sr_mgmt +
'&submitted_by=' + submitted_by +
'&classof=' + classof +
'&school=' + school +
'&anonymous=' + anonymous_tmp;
alert (dataString);
$.ajax(
{
type:"POST",
url:"process.php",
data:dataString,
success:function()
{
$('#testimonials').html("<div id='message'></div>");
$('#message').html("<h2>Your information has been submitted!</h2>")
.append("<p>Thank you for your help and support.</p>")
.hide()
.fadeIn(1500, function()
{
$('#message').append("<img id='checkmark' src='images/check.png'/>");
});
}
});
return false;
});
});
process.php 是否返回 500 错误,它是否位于您的 web 应用程序的根目录中? 您可以通过检查浏览器中的开发者控制台日志找到,或者更改 function
success:function()
到
success:function(res, textStatus, jqXHR) {
console.log(res);
console.log(textStatus);
console.log(jqXHR);
}
并检查返回的内容。
此外,而不是手动构建表单值,你为什么不直接使用
$('id_of_form').serialize();
然后将它传递给 $.post?
希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.