【问题标题】:Form submit is not sending any POST data表单提交未发送任何 POST 数据
【发布时间】:2013-12-01 03:38:32
【问题描述】:

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

  • 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>

【问题讨论】:

  • 如果你已经在使用 jQuery,为什么要document.forms['formBillingOwner'].elements['selectedBillingOwner'].value 而不是$('#selectedBillingOwner').val()
  • 我补充说是因为我很困惑,想确定这些字段是否已设置,并认为我的 jquery 可能不正确。

标签: php jquery html forms post


【解决方案1】:

简单,因为您的表单字段没有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>

为它们添加名称,例如:

<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>

注意: 可能你的 jQuery 分配也需要修复,但如果这是唯一的问题,那么至少一个错误的值应该是 POSTed 到 PHP,因此不是 问题。

【讨论】:

  • 谢谢!!我认为这将是一件小事和愚蠢的事情。现在我不觉得很傻!!如果我告诉你我做网页多久了,我会更尴尬。阅读您的答案后,我心想“我并不总是包含 'name' 属性”,但它们存在于我查看过的其他页面中。
  • 干杯,发生在我们所有人身上
  • 你救了我的工作 :-D @HankyPanky
【解决方案2】:

删除 jQuery 的这两行代码。

$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");

因为这会在您提交表单之前更改文本字段的值

【讨论】:

  • “不要在咖啡里加糖,加糖会变甜。”好吧,如果我不想要它甜,我就不会这样做。 :P
猜你喜欢
  • 1970-01-01
  • 2017-09-06
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
相关资源
最近更新 更多