表单提交不发送任何POST数据

dar*_*t78 2 html php forms jquery post

在我的网页上,当我按"添加客户"链接时,会发生以下情况:

  • 调用onclick处理程序,其中
    • 将值设置为表单中的两个文本字段
    • 显示警报(此时您可以在屏幕上的文本字段中看到新值)
    • 调用表单的提交按钮(注意:表单提交回自己的PHP文件)
  • 调用相同的php文件,但没有收到POST值
    • 我已经证实了这一点
      • 第一行中的代码计算$ _POST中的值,然后显示
      • 使用fiddler查看发送到页面的请求

我以前做过很多次这种事情都没有问题.我已将此代码复制到两个不同的Web服务器(linux,apache),结果相同.这可能是一个小问题,但我找不到它.

我已经删除了一大堆代码来解决这个问题,但还没弄清楚为什么没有发送POST值.

您可以在http://www.daleann.org/dla.php上查看此工作副本.除了以下代码之外,唯一需要的是/js/jquery.min.js.

谢谢你的帮助.

<?php
$pc=count($_POST)."<br />".date("H:i:s");
?>
<html>
<head>
<script src="/js/jquery.min.js" /></script>
<script type="text/javascript">
$(document).ready(function() {
  alert("inside docReady");
  $(document).on('click', "a.menuBillingOwner", function() {
    $("#selectedBillingOwner").val("11");
    $("#lastCustomNavSelected").val("selectedBillingOwner");
    alert("selectedBillingOwner = "+document.forms['formBillingOwner'].elements['selectedBillingOwner'].value);
    document.forms['formBillingOwner'].submit();
  });
});
</script>
</head>
<body>
  <ul id="menuBillingOwner">
    <li><a href='#' id='menuBillingOwnerAdd' class='menuBillingOwner'>Add Customer</a></li>
  </ul>
  <?php
  $lastCustomNavSelected = $selectedBillingOwner = "";
  if (count($_POST) > 0 && isset($_POST['selectedBillingOwner'])) {
     $lastCustomNavSelected = "selectedBillingOwner";
     $selectedBillingOwner = $_POST['selectedBillingOwner'];
  }
  ?>
  <?php echo "pc = ".$pc."<br />\n"; ?>
  <form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
    <input type="text" id="lastCustomNavSelected" value="<?php echo      $lastCustomNavSelected; ?>" />
    <input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
  </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Han*_*nky 10

很简单,因为您的表单字段没有name属性,请参阅下文

<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
    <input type="text" id="lastCustomNavSelected" value="<?php echo      $lastCustomNavSelected; ?>" />
    <input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
  </form>
Run Code Online (Sandbox Code Playgroud)

为它们添加名称,例如:

<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
    <input type="text" id="lastCustomNavSelected" name="lastCustomNavSelected" value="<?php echo      $lastCustomNavSelected; ?>" />
    <input type="text" id="selectedBillingOwner" name="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
  </form>
Run Code Online (Sandbox Code Playgroud)

注:可能需要您的jQuery的分配进行过固定的,但如果这是唯一的问题,那么ATLEAST一个错误的值应该已经POST编到PHP,因此这不是问题.