What I have:
- I have a form with two normal text boxes and two disabled text boxes.
- The disabled elements receive their values from the enabled elements via jQuery.
- The values of the four elements are then posted via PHP
Note: My form is being used as part of acustom WordPress page.
The problem:
The disabled elements (i.e. values derived via jQuery) are not posting via PHP.
Visual:
My code:
PHP:
<?php
if(isset($_POST['submit'])) {
echo 'a= '.$_POST["textbox_a"].'<br />';
echo 'b= '.$_POST["textbox_b"].'<br />';
echo 'c= '.$_POST["textbox_c"].'<br />';
echo 'd= '.$_POST["textbox_d"].'<br />';
}
?>
jQuery:
<script type="text/javascript">
jQuery(document).ready(function($) {
// Code here will be executed on document ready. Use $ as normal.
// alert("jQuery is working fine :-)");
$( "button" ).click(function( event ) {
event.preventDefault();
$('[name=textbox_c]').val($('[name=textbox_a]').val());
$('[name=textbox_d]').val($('[name=textbox_b]').val());
});
});
</script>
HTML:
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="input" name="textbox_a" />
<input type="input" name="textbox_b" />
<button>[jQuery] Click me to transfer</button><br />
<input type="input" name="textbox_c" disabled="disabled" />
<input type="input" name="textbox_d" disabled="disabled" />
<input type="submit" name="submit" value="[PHP] Click me to post" /><br />
</form>
My question:
Why isn’t PHP treating my jQuery derived values as regularly inputted values? Though I’ve never used Ajax, am I correct in assuming that Ajax is the solution? The examples I’ve seen for Ajax seem a little overkill for what I’m trying to achieve but I’m happy to delve into it if it’s the only way forward.
use
readonly
instead ofdisabled
From Mozilla:
Disabled inputs will not be submitted with the form; that’s part of the defined behaviour of disabled.
Disabled elements do not get sent to the server. You can use
readonly
instead ofdisabled
, the difference isreadonly
inputs are sent to the server.I don’t believe disabled form elements actually ever get posted (regardless of being dynamically created by jQuery). I would use the
readonly="readonly"
attribute instead.