Muhammad Aatif
Muhammad Aatif

Reputation: 27

check a radio button based on value retrieved from database

I have radio buttons that want to check if value retrieved from database is 'Yes'. on this same forum a same question is there but after trying that and still no success. also i can't comment there anything about my question because i don't have sufficient reputation. link to the question same as mine is: How to set the value for Radio Buttons When edit?

Below is the Form code which displays fetched data:

<?php
error_reporting(0);
$server="localhost";
$user="root";
$password="";
$database="camouflage_studio";

$con = mysqli_connect($server,$user,$password,$database);
if (mysqli_connect_errno())
  {
  echo "Connection Error: " . mysqli_connect_error();
  }
//receiving CINC from search form and getting record on it's basis
$cnic = $_POST['search'];
$data = "SELECT * FROM personal_detail WHERE CNIC='$cnic'";
if($query = mysqli_query($con, $data)){
      $data2 = mysqli_fetch_array($query);  //i think it's used for to know the total number of rows fetched/retrieved 
}else{ echo "Query Not Executed!";}
?>
<!DOCTYPE html>
<html>
<head>
<title>Camouflage Studio - Welcome</title>
<!-- My CSS coding starts here -->
</head>
<body>
<!-- form to display Retrieved/Fetched data-->
<center>
<form action="update.php" method="post">
<fieldset style="width:50%"><legend>Please do the required changes</legend><br>
<label for="Name">Name :<br></label><input name="name" type="text" size="20" maxlength="40" value="<?php echo $data2[Name]?>"><br>
<label for="CNIC">CNIC :<br></label><input name="cnic" type="text" size="20" maxlength="15" value="<?php echo $data2[CNIC]?>"><br>
<label for="Date">Booking Date :<br></label><input name="booking-date" type="date" size="20" value="<?php echo $data2[Date]?>"><br>

<!-- <label for="Ocassion">Ocassion :<br></label> -->
<label for="Ocassion">Ocassion :<br></label><input name="ocassion" type="text" size="20" maxlength="15" value="<?php echo $data2[Ocassion]?>"><br>

<label for="Address">Address :<br></label><input name="address" type="text" size="20" maxlength="11" value="<?php echo $data2[Address]?>"><br>

<label for="Phone Number">Phone Number :<br></label><input name="phone-no" type="text" size="20" maxlength="11" value="<?php echo $data2[Phone_No]?>"><br>
<label for="Bride Mobile">Bride Mobile :<br></label><input name="bride-mobile" type="number" size="20" maxlength="11" value="<?php echo $data2[Bride_Mobile]?>"><br>
<label for="Groom Mobile">Groom Mobile :<br></label><input name="groom-mobile" type="number" size="20" maxlength="11" value="<?php echo $data2[Groom_Mobile]?>"><br>
<label for="Family Mobile">Family Mobile :<br></label><input name="family-mobile" type="number" size="20" maxlength="11" value="<?php echo $data2[Family_Mobile]?>"><br>
<label for="Email">Email :<br></label><input name="email" type="text" size="20" maxlength="30" value="<?php echo $data2[EMail]?>"><br>
<label for="Who may I Thank for Refering You?">Who may I Thank for Refering You? :<br></label><input name="refering" type="text" size="20" maxlength="40" value="<?php echo $data2[Referring]?>"><br>
<label for="Do you provide consent to share images on our official web page">Do you provide consent to share images on our official web page? :<br><br></label><input type="radio" name="share" <?php echo ($data2[Share]=='Yes')?'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="share" <?php echo ($data2[Share]=='Yes')?'checked':'' ?> value="No">No<br><br>
<label for="If yes, with Identity">If yes, with Identity? :<br><br></label><input type="radio" name="permission" <?php echo ($data2[Permission]=='Yes')?'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="permission" <?php echo ($data2[Permission]=='Yes')?'checked':'' ?> value="No">No<br><br>
<!-- To center the button i'm embedding the buttons in a paragraph with an id as well. the id is used for CSS in head -->
<p id="btn">
<input type="submit" value="Update Record" name="submit_display_data_form" style="font-size:16px"></p>
</fieldset>
</form>
</center>
</body>
</html>

Upvotes: 2

Views: 8178

Answers (2)

Pierre Granger
Pierre Granger

Reputation: 2012

You have to understand what value will be stored in $_POST['key1']. If nothing is checked, $_POST['key1'] doesn't exist : !isset($_POST['key1']) ; If first radio is checked, $_POST['key1'] = 'Yes' ; If second radio is checked, $_POST['key1'] = 'No' ; If you want to pre-check "No" when nothing is checked, you should test if the data does not exist or if the value is anything except 'Yes'.

In that way, you're sure that you will have 'Yes' or 'No' after submitting the form.

<input type="radio" name="key1" value="Yes" <?php if ( isset($data['key1']) && $data['key1'] == 'Yes' ) echo 'checked' ; ?> />

<input type="radio" name="key1" value="No" <?php if ( ! isset($data['key1']) || $data['key1'] !== 'Yes' ) echo 'checked' ; ?> />


<input type="radio" name="key2" value="Yes" <?php if ( isset($data['key1']) && $data['key2'] == 'Yes' ) echo 'checked' ; ?> />

<input type="radio" name="key2" value="No" <?php if ( ! isset($data['key1']) || $data['key2'] !== 'Yes' ) echo 'checked' ; ?> />

Upvotes: 0

user5003187
user5003187

Reputation:

$Share = 'Yes';
$Permission = 'Yes';
    <label for="Do you provide consent to share images on our official web page">
        Do you provide consent to share images on our official web page? :
    </label>
    <input type="radio" name="share" <?php echo ($Share =='Yes')? 'checked':'' ?> value="Yes">Yes<br>
    <input type="radio" name="share" <?php echo ($Share =='No')? 'checked':'' ?> value="No">No<br><br>

    <label for="If yes, with Identity">If yes, with Identity? :<br><br></label>
    <input type="radio" name="permission" <?php echo ($Permission=='Yes')?'checked':'' ?> value="Yes">Yes<br>
    <input type="radio" name="permission" <?php echo ($Permission=='No')?'checked':'' ?> value="No">No<br><br>

Upvotes: 2

Related Questions