Clarus Dignus
Clarus Dignus

Reputation: 3897

PHP not posting textbox values produced by jQuery

What I have:

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:

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.

Upvotes: 1

Views: 423

Answers (4)

Adunahay
Adunahay

Reputation: 1621

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.

Upvotes: 4

Jared Cobb
Jared Cobb

Reputation: 5267

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.

Upvotes: 1

MrCode
MrCode

Reputation: 64526

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.

Upvotes: 1

jens_vdp
jens_vdp

Reputation: 594

Disabled inputs will not be submitted with the form; that's part of the defined behaviour of disabled.

Upvotes: 3

Related Questions