Annie
Annie

Reputation: 37

radio default button auto submit

I have created a form using radio button and using checked for default radio button.i want to auto submit default button. But i'm not getting it. Here is my code -

 <p><u>Location<u></p>
                <form method="post" action="amazon.php">
                <div class="form-group">
                <div class="radio">
                <label><input type="radio" name="level" value="B1" <?php if (isset($_POST[ 'level']) && $_POST[ 'level']=='B1' )?>onclick="this.form.submit()">Basement 1</label>
                </div>
                <div class="radio">
                <label><input type="radio" name="level" <?php if (isset($_POST[ 'level']) && $_POST[ 'level']=='L1' )?> value="L1"checked="checked"onchange="this.form.submit();">Level 1</label>
                </div>  
                </div>
                </form>

Upvotes: 0

Views: 590

Answers (2)

Pupil
Pupil

Reputation: 23948

Change the event from onchange to onclick.

Corrected Code:

<label><input type="radio" name="level" 
<?php if (isset($_POST[ 'level']) && $_POST[ 'level']=='L1' )?> 
value="L1" checked="checked" onclick="this.form.submit();">Level 1</label>

You have default checked the L1 radio.

And onchange event is called on it.

If you click on L1, it should be checked if not checked.

But, being already checked, no change happening, therefore, no form submit is occurring.

If you change the event to click, it will trigger on click only irrespective of it is checked or unchecked..

Upvotes: 0

sathish R
sathish R

Reputation: 422

I think the problem is there is no space between the value="L1"checked="checked" on the second radio causes the error. And use radio button group to group the radio buttons.

Upvotes: 1

Related Questions