PHP not posting textbox values produced by jQuery

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.

Read More

Visual:

enter image description here

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.

Related posts

Leave a Reply

4 comments

  1. use readonly instead of disabled

    From Mozilla:

    Disabled

    This Boolean attribute indicates that the form control is not available for interaction. In particular, the click event will not be dispatched on disabled controls. Also, a disabled control’s value isn’t submitted with the form.

  2. The disabled elements (i.e. values derived via jQuery) are not posting via PHP.

    Disabled elements do not get sent to the server. You can use readonly instead of disabled, the difference is readonly inputs are sent to the server.

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