使用javascript设置输入值会使PHP帖子无效

时间:2013-09-19 23:17:31

标签: javascript php jquery forms

我有一个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变量为null。表单中的每个其他输入都有效,如果我删除了javascript并手动输入我使用JavaScript设置的完全相同的文本,它也能正常工作。

问题实际上是为什么用javascript填充字段而不是手动在文本输入中输入完全相同的字符串会破坏事情。同样没有错误,输入正确填充在前端。以某种方式发布表单只是在javascript与手动键入时设置的值没有。我必须在这里找到一些非常基本的东西。

知道这里发生了什么吗?我花了好几个小时对此感到困惑无济于事。

更新: 代码更新,测试页面: http://dev.rocdesign.info/test/

1 个答案:

答案 0 :(得分:0)

解决方案:无法发布已禁用的输入。我实际上在开始时测试了它,并且一定错过了删除输入上的“禁用”使它工作,所以我错误地将其排除并继续前进。

感谢大家的回复。对于有这个问题的其他人:使用隐藏的输入来发布值。