aron pascua
aron pascua

Reputation: 111

How can I echo out the value stored in my radio button from my database?

I'm trying to echo out the current value of the gender of particular data and edit the gender if the user want to. How can I do that with this code?

Here is my code:

    echo "Gender: <label class=\"radio-inline\"><input type=\"radio\" name=\"gender\" value=\"Male\" > Male</label>
                  <label class=\"radio-inline\"><input type=\"radio\" name=\"gender\" value=\"Female\"> Female</label><br/>";

Below is the whole code for how I get all the information in my database:

       <?php 
        while(mysqli_stmt_fetch($stmt1)) {
        echo "First Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$first_name\"><br/>";
        echo "Middle Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$middle_name\"><br/>";
        echo "Last Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$last_name\"><br/>";
        echo "Age: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$age\"><br/>";
        echo "Birth Date: <select name=\"month\"  style=\"width:auto;\">
                          <option value=\"$month\"></option>
                          <option value=\"January\">January</option>
                          <option value=\"February\">February</option>
                          <option value=\"March\">March</option>
                          <option value=\"April\">April</option>
                          <option value=\"May\">May</option>
                          <option value=\"June\">June</option>
                          <option value=\"July\">July</option>
                          <option value=\"August\">August</option>
                          <option value=\"September\">September</option>
                          <option value=\"October\">October</option>
                          <option value=\"November\">November</option>
                          <option value=\"December\">January</option>
                          </select>
                          <select name=\"day\" style=\"width:auto;\">
                          <option value=\"\"></option>
                          <option value=\"1\">1</option>
                          <option value=\"2\">2</option>
                          <option value=\"3\">3</option>
                          <option value=\"4\">4</option>
                          <option value=\"5\">5</option>
                          <option value=\"6\">6</option>
                          <option value=\"7\">7</option>
                          <option value=\"8\">8</option>
                          <option value=\"9\">9</option>
                          <option value=\"10\">10</option>
                          <option value=\"11\">11</option>
                          <option value=\"12\">12</option>
                          <option value=\"13\">13</option>
                          <option value=\"14\">14</option>
                          <option value=\"15\">15</option>
                          <option value=\"16\">16</option>
                          <option value=\"17\">18</option>
                          <option value=\"19\">19</option>
                          <option value=\"20\">20</option>
                          <option value=\"21\">21</option>
                          <option value=\"22\">22</option>
                          <option value=\"23\">23</option>
                          <option value=\"24\">24</option>
                          <option value=\"25\">25</option>
                          <option value=\"26\">26</option>
                          <option value=\"27\">27</option>
                          <option value=\"28\">29</option>
                          <option value=\"30\">30</option>
                          <option value=\"31\">31</option>
                          </select>
                          <input type=\"text\" value=\"$year\" size=\"3\" style=\"border: none; border-color: transparent;\"><br/>";
        echo "Birth Place: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$birth_place\"><br/>";
        echo "Gender: <label class=\"radio-inline\">
              <input type=\"radio\" name=\"gender\" value=\"Male\" ";
              if($gender == 'Male') { echo 'Checked';}
              echo " > Male</label>
              <label class=\"radio-inline\">
              <input type=\"radio\" name=\"gender\" value=\"Female\" ";
              if($gender == 'Female') { echo 'Checked';}
              echo "> Female</label><br/>";
        echo "Marital Status: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$martial_status\"><br/>";
        echo "Religion: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$religion\"><br/>";
        echo "Nationality: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$nationality\"><br/>";
        echo "Email Address: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$email_address\" size=\"35\"><br/>";
        echo "Address 1: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$address_1\"><br/>";
        echo "Address 2: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$address_2\"><br/>";
        echo "Course: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$course\"><br/>";
        echo "School Graduated: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$school_graduated\" size=\"65\"><br/>";
        echo "Remarks: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$remarks\"><br/>";
        echo "<br/><br/>";
        echo "<center><a href=\"view_all_crew.php\"><button type=\"button\" class=\"btn btn-default\" >Cancel</button></a>&nbsp;";
        echo "<button type=\"button\" class=\"btn btn-success\" onclick=\"document.location = 'edit_crew.php?id=$id'; \">Continue</button></center>";
      }
      ?>

Upvotes: 0

Views: 647

Answers (3)

PHP Geek
PHP Geek

Reputation: 4033

Suppose you stored 1 for male and 0 for female in your database table then then the code fetched from your database can be implemented as :

<input type="radio" name="gender" <?php echo $row['gender'] === '1' ?'checked="checked"':'';?>/> Male
<input type="radio" name="gender" <?php echo $row['gender'] === '0' ?'checked="checked"':'';?>/> Female

Thats it. If male is stored in the database then first one will be checked else second one will be checked.

Upvotes: 1

JYoThI
JYoThI

Reputation: 12085

try something like this use single quotes for echo and use double quotes for values its easy to do .

    <?php

    $gender = 'Male';

    echo 'Gender: <label class="radio-inline">
                   <input type="radio" name="gender" value="Male"';
                   if(isset($gender) && $gender == 'Male') { echo 'checked="checked"'; }
     echo ' > Male</label>
                  <label class="radio-inline">
                  <input type="radio" name="gender" value="Female" ';
                  if(isset($gender) && $gender == 'Female') { echo 'checked="checked"'; }
      echo '> Female</label><br/>';

    ?>

Upvotes: 0

ASR
ASR

Reputation: 1811

Use an if condition in radio button to select your stored value like

$gender = 'Female';
echo "Gender: <label class=\"radio-inline\">
               <input type=\"radio\" name=\"gender\" value=\"Male\" ";
               if(isset($gender) && $gender == 'Male') { echo 'checked';}
 echo " > Male</label>
              <label class=\"radio-inline\">
              <input type=\"radio\" name=\"gender\" value=\"Female\" ";
              if(isset($gender) && $gender == 'Female') { echo 'checked';}
  echo "> Female</label><br/>";

Upvotes: 1

Related Questions