ak111in
ak111in

Reputation: 107

Radio button value not updating in PHP

I have 4 radio buttons in my form:

<tr><td>Type</td><td>
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D</td></tr>

On page load I set one of the radio buttons using jquery

$("#b").prop("checked", true);

Now I select the value d in my form and submit. In PHP I echo $_POST['type'] , I am always getting the value which was set during page load using jquery i.e. in this case b instead of d.

Why is the value not updating?

Thanks.

UPDATE:Thanks all, it was due to unintentional val() called on radio button. So if radio button value is set using val() it will not change later, strange behavior.

Upvotes: 2

Views: 1696

Answers (5)

GTS Soft.
GTS Soft.

Reputation: 177

Try this code

<?php
if(isset($_REQUEST['sb']))
{
    echo $_REQUEST['type'];
}
?>
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(e) {
    $('#b').attr('checked','checked');
});
</script>
<tr><td>Type</td><td>
<form name="frm" method="post" action="">
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D</td></tr>
<input type="submit" name="sb" value="submit" />
</form>
</body>
</html>

Upvotes: 0

thedom
thedom

Reputation: 2518

Works like a charm ;-)).

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js" /></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#b').attr('checked', 'checked');
        });
        </script>
    </head>

    <body>
<?php
if(isset($_POST['sbmt']) && isset($_POST['type'])) {
?>
        <h1>Selected type: <?php echo($_POST['type']); ?></h1>
<?php
}
?>

        <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
            <ul>
                <li><input type="radio" name="type" id="a" value="a" /> A</li>
                <li><input type="radio" name="type" id="b" value="b" /> B</li>
                <li><input type="radio" name="type" id="c" value="c" /> C</li>
                <li><input type="radio" name="type" id="d" value="d" /> D</li>
            </ul>

            <input name="sbmt" type="submit" value="Submit" />
        </form>
    </body>
</html>

Upvotes: 1

cb0
cb0

Reputation: 8613

In jQuery 1.6+

$('#b').prop('checked', true);
$('#b').prop('checked', false);

jQuery 1.5 and below

$('#b').attr('checked','checked');
$('#b').removeAttr('checked');

Upvotes: 2

rack_nilesh
rack_nilesh

Reputation: 553

instead of using

$("#b").prop("checked", true);

why dont you write your radio buttons as

<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" checked="checked"  >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D

Upvotes: 1

dhalfageme
dhalfageme

Reputation: 1545

Try:

 $("#b").attr("checked", true);

or

     $("#b").attr("checked", "checked");

Upvotes: -1

Related Questions