首页 > 解决方案 > 无法使用 jQuery .submit() 获取 $_POST('input')

问题描述

我尝试使用 jQuery 上传表单submit()并成功将页面重定向到 uploadAll.php,但是当我尝试使用获取输入数组$_POST['inputname']时无法获取值。

我试图使用serialize(),但我对应该放置 PHP 页面参考的位置感到困惑。

<form id="regForm" action="uploadAll.php" method="post" enctype="multipart/form-data">
  //some inputs and a hidden input here
</form>
document.getElementById("regForm").submit(function() {
  var inputValues = $(this).serialize();

  $.ajax({
    url: "uploadAll.php",
    type: "POST",
    data: inputValues
  }).done(function(data) {
    alert(data);
  });
});

return false;

我可以不使用就提交serialize()吗?看来我的序列化不起作用。任何帮助表示赞赏

标签: javascriptphpjquery

解决方案


相信我,你的代码是错误的..

尝试使用此代码:

$("#regForm").on('submit', function(e) {
  e.preventDefault();
  var inputValues = $(this).serialize();

  $.ajax({
    url: "uploadAll.php",
    type: "POST",
    data: inputValues
  }).done(function(data) {
    alert(data);
  });
});

推荐阅读