在我的网页上,当我按"添加客户"链接时,会发生以下情况:
- 调用 onclick 处理程序,它
- 将值设置为"表单两个"文本字段
- 显示警报(此时,您可以在屏幕上的文本字段中看到新值)
- 调用表单的提交按钮(注意:表单提交回它自己的 PHP 文件)
- 调用了相同的 php 文件,但没有收到 POST 值
- 我已经验证了这一点
- 第一行中以 $_POST 为单位计算值的代码,然后稍后显示
- 使用提琴手查看发送到页面的请求
- 我已经验证了这一点
我以前做过很多次这种事情,没有问题。我已将此代码复制到两个不同的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,因此这不是问题。
删除 jQuery 的这 2 行。
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
因为这会在您提交表单之前更改文本字段的值