-1

I am using forms in html there are some radio buttons in it too, when the form is incomplete and user press submit button, server redirects(using header) the user to the form again and says to fill the for completely, when the user came back to the page the data he had already filled it should have been there, i have done this in case of name and email but it is difficult in case of radio buttons

header("location:main.php?nm=$name&email=$em&gender=$gen&paf=$paf&msg=please fill form");

form code

Gender: <input type="radio" name="gender" value="Male" >Male <input type="radio" name="gender" value="Female" >Female <input type="radio" name="gender" value="Other" >Other

  • 1
    Possible duplicate of [How to pre-select a radio button in jQuery](https://stackoverflow.com/questions/27806098/how-to-pre-select-a-radio-button-in-jquery) – Akhil Mathew Jan 01 '18 at 07:04
  • 1
    Possible duplicate of [How to select a radio button by default?](https://stackoverflow.com/questions/5592345/how-to-select-a-radio-button-by-default) – 4castle Jan 01 '18 at 07:08
  • @AKHILMATHEW it is not the duplicate of any of these my question was about if we return to the previous form page i want the options i had selected before should be selected now – Muhammad Ubaid Ullah Farooqi Apr 09 '18 at 07:47

3 Answers3

4

You can use checked property

<input type="radio" name="name" value="" checked="checked" />
Hasan Ali
  • 252
  • 4
  • 13
2

  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other<br><br>
TarangP
  • 2,711
  • 5
  • 20
  • 41
0

on the form page in php code u have to set variables of $male, $female to checked like this.

if (!empty($_GET['gender'])){
if($_GET['gender']=="Male"){
    $male="checked";
}
else if($_GET['gender']=="Female"){
    $fmale="checked";
}
else{
    $other="checked";
}}

and in the form echo those variables

    Gender:
  <input type="radio" name="gender" value="Male" <?php echo $male; ?>>Male
  <input type="radio" name="gender" value="Female" <?php echo $fmale; ?> >Female
  <input type="radio" name="gender" value="Other" <?php echo $other; ?> >Other