在我的网页上,当我按下“添加客户”链接时,会发生以下情况:
- 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>
最佳答案
很简单,因为您的表单字段没有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 赋值也需要修复,但如果这是唯一的问题,那么至少应该将错误的值POST
发送到 PHP,因此不是问题。
关于php - 表单提交不发送任何 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040138/