1

我不确定我做错了什么。我搜索了多个论坛,但没有得到解决方案。我为从表单提交值定义了以下 ajax。我有警报功能以确保正在调用此代码(确实如此),但没有执行 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;
});
});
4

1 回答 1

1

process.php 是否返回 500 错误,它是否位于 Web 应用程序的根目录中?您可以通过在浏览器中查看开发者控制台日志来查找,或者更改功能

success:function() 

success:function(res, textStatus, jqXHR) {
console.log(res);
console.log(textStatus);
console.log(jqXHR);
}

并检查返回的内容。

此外,与其手动构造表单值,不如直接使用

$('id_of_form').serialize();

并将其传递给 $.post?

希望有帮助。

于 2012-04-10T01:58:42.120 回答