Thursday, 19 September 2013

Setting input value with javascript nullifies PHP post

Setting input value with javascript nullifies PHP post

I 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). The way the
form basically works (radically simplified) is this:
<?php
$email = $_POST['email'];
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" id="email" name="email" value="<?php echo
$_POST['email']; ?>" />
</form>
And then in jQuery or pure JS I do one of these:
$('#email').val('newValue');
or
document.getElementById('newValue').value = 'newValue';
Both of the above seem to work. They populate the input with the value I
want. But 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.
Any idea what's going on here? I've spent hours puzzling over this to no
avail.

No comments:

Post a Comment