Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I a have PHP form where I collect a bunch of values from text inputs, but for one input I have the input filled in via javascript (user selects a date from a calendar, that date then populates a text input). I've setup a simplified version of this:

<?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 = "[email protected]";

    $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: [email protected]";

    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.
?>

And then in jQuery I do one of these:

$('.note').click(function(){
$('#installation-time').val('test string');
});

When I submit the form, the PHP variable that's supposed to collect that value is null. Every other input in the form works, and if I remove the javascript and manually enter the exact same text that I had set with JavaScript into the input it works as well.

The question really is why populating a field with javascript as opposed to manually typing the exact same string into a text input would break things. Again there are no errors and the input is populated correctly on the front end. Somehow posting the form just doesn't pick up on the value when it's set by javascript vs. typed manually. There has to be something really fundamental I'm missing here.

Any idea what's going on here? I've spent hours puzzling over this to no avail.

Update: Code updated, test page: http://dev.rocdesign.info/test/

share|improve this question
 
Sounds like the form is losing the dynamic value because it posts to itself. When the page loads for the POST the calendar is not picked so the field is blank? –  andrewb Sep 19 '13 at 23:19
 
Check your submit routine for errors. –  jeff Sep 19 '13 at 23:21
 
@Jeff, there's no errors that I can find. –  tD3rk Sep 19 '13 at 23:22
1  
Actually your jQuery and pure JS are doing different things... Maybe just a typo in your post? –  Teemu Sep 19 '13 at 23:41
1  
The information that you have provided is not enough. Your problem is in a place you have not written here, please give link. –  Neo Sep 20 '13 at 0:24
show 5 more comments

1 Answer

up vote 0 down vote accepted

Solution: can't post a disabled input. I actually tested that back in the beginning and must have missed that removing the "disabled" on the input made it work, so I mistakenly ruled it out and moved on.

Thanks for the responses everyone. And for anyone else with this problem: use a hidden input to post the value.

share|improve this answer
1  
Some browsers will post it still... beware –  Salketer Sep 20 '13 at 15:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.