我有一个 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/