我有一个PHP表单,我从文本输入中收集一堆值,但是对于一个输入,我通过javascript填写了输入(用户从日历中选择一个日期,然后该日期填充文本输入)。我已经设置了一个简化版本:
<?php
$displayForm = true;
if ($_POST['submitFlag'] == 1) {
// Form was submitted. Check for errors and submit.
$displayForm = false;
$installationTime = $_POST['installation-time'];
// send e-mail notification
$recipients = "test@test.com";
$subject = "Test Email - Test Form Submission";
$message = wordwrap('Someone has filled out the secure form on test.com. Here''s what they had to say:
Installation Time: ' . $installationTime .'
');
$headers = "From: test@test.com";
mail($recipients, $subject, $message, $headers);
// Output thank you message
?>
<h2>Thank You!</h2>
<?php if($installationTime == NULL){echo 'test failed: value submitted was null.';}else{echo 'test passed: value submitted was not null.';} ?>
<p>Your form has been submitted. Thank you for your interest in test.com.</p>
<?php
}
if ($displayForm) {
// If form was not submitted or errors detected, display form.
?>
<div class="note"><span class="required">*</span> Click me to set value of input.</div>
<form name="contactForm" id="contactForm" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>?state=submit">
<label for="installation-time" class="service-time">The time you have selected for installation is: <span class="required">*</span></label>
<input type="text" name="installation-time" id="installation-time" disabled value="<?php echo $_POST['installation-time']; ?>" />
<input type="hidden" name="submitFlag" id="submitFlag" value="1" />
<input type="submit" name="submit" id="submit" value="Sign-Up" />
</form>
<?php
} // End of block displaying form if needed.
?>
然后在jQuery中,我做了其中之一:
$('.note').click(function(){
$('#installation-time').val('test string');
});
当我提交表单时,应该收集该值的 PHP 变量为空。表单中的所有其他输入都有效,如果我删除 javascript 并手动将我使用 JavaScript 设置的完全相同的文本输入到输入中,它也可以工作。
真正的问题是,为什么用javascript填充字段而不是在文本输入中手动输入完全相同的字符串会破坏事情。同样没有错误,输入在前端正确填充。不知何故,发布表单只是在由 javascript 设置与手动键入时不会获取值。我在这里一定缺少一些真正基本的东西。
知道这里发生了什么吗?我花了几个小时对此感到困惑,但无济于事。
更新:代码已更新,测试页:http://dev.rocdesign.info/test/
解决方案:无法发布禁用的输入。我实际上在一开始就测试过它,并且一定错过了删除输入上的"禁用"使其工作,所以我错误地排除了它并继续前进。
感谢大家的回复。对于有此问题的其他任何人:使用隐藏的输入来发布值。