Sithelo Ngwenya
Sithelo Ngwenya

Reputation: 501

Radio button checked by default but un-checked on submit

I have a a group of radio button as shown below. I intend to have a situation where on initial load of page the default radio button checked is plastic. The code below just retains my selected radio button.

<div id="material">
<input type="radio"  name="materials" value="glass" style="vertical-align: middle"
  onchange="this.form.submit()"
  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>
  />
<label for="material">Glass</label> <br/>

<input type="radio"  name="materials"  style="vertical-align: middle" value="plastic"
  onchange="this.form.submit()"
  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>
/>
<label for="material" >Plastic (Organic)</label>
</div>

Upvotes: 0

Views: 9363

Answers (3)

sandy
sandy

Reputation: 1158

Try the code as given below, it has the desired change

<div id="material">
    <input type="radio"  name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
     <label for="material">Glass</label> <br/>

    <input type="radio" checked="checked"   name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
    <label for="material" >Plastic (Organic)</label>
    </div>

UPDATED

<form method="post" >
 <div id="material">
 <?php if(isset($_POST['materials'])) {
    if($_POST['materials'] == 'glass'){
    ?>
<input type="radio" checked="checked" name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
 <label for="material">Glass</label> <br/>
 <input type="radio"   name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
<label for="material" >Plastic (Organic)</label>
 <?php } 
 else { ?>
  <input type="radio"  name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
  <label for="material">Glass</label> <br/>
 <input type="radio" checked="checked"  name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
 <label for="material" >Plastic (Organic)</label>
 <?php } 
 } else { ?>
 <input type="radio"  name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
 <label for="material">Glass</label> <br/>
<input type="radio" checked="checked"  name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
<label for="material" >Plastic (Organic)</label>
<?php } ?>
</div>

<input type="submit" name="submit">
</form>

Upvotes: 0

Samuel Liew
Samuel Liew

Reputation: 79022

Remove onchange="this.form.submit()" from the radio buttons, and add this between script tags in your page header:

<script>
window.onload = function() {
    // when the form is submitted
    document.forms[0].onsubmit = function() {
        // get all radio buttons with name = materials
        var radios = document.getElementsByName('materials');
        // for each radio button
        for(i=0; i<radios.length; i++) {
            // uncheck radio buttons
            radios[i].checked = false;
        }
    };
};
</script>

Upvotes: 1

duellsy
duellsy

Reputation: 8577

Try changing "checked" to 'checked="checked"'

Upvotes: 0

Related Questions