Retrieve mysql value from php for radio buttons
The HTML
attribute checked
should not get a value, its mere presence indicates that the radio button is checked. So do this:
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {echo "checked"}?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {echo "checked"}?> value="B">B
Or using the more compact short echo tag <?= .. ?>
and ternary operator:
<label>Owner: <?=$row['Owner']?></label></br>
<input type="radio" name="Owner" <?=$row['Owner']=="A" ? "checked" : ""?> value="A">A
<input type="radio" name="Owner" <?=$row['Owner']=="B" ? "checked" : ""?> value="B">B
Note that you need double equals signs for comparisons.
How to retrieve value from MYSQL to radio button using PHP?
Wouldn't you want to make it a little easier & manageable by simply moving the Conditionals out of the Radio Buttons Fields? Creating 2 Variables & assigning values to them based on some conditions wouldn't cost you so much & would rather make things much clearer. Here, attempt was made to just create 2 variables: $checkYes & $checkNo before echoing out any thing. Then we checked if $result_position[$i]->process_resume=="Yes" in which case, we assigned "checked" to the $checkYes Variable. We did the same for NO. Afterwards, we then added the $checkYes or $checkNo at the appropriate slots...
<?php
// BE SURE THAT BOTH THE VARIABLES & CONDITIONAL STATEMENTS EXIST
// WITHIN YOUR LOOP... PREFERABLLY AT THE BEGINNING OF THE LOOP.
// OTHERWISE; $i HAS NO MEANING TO THE CONDITIONAL STATEMENTS BELOW.
// CREATE THE $checkYes & $checkNo VARIABLES + INITIALIZE THEM TO EMPTY STRINGS
$checkYes = "";
$checkNo = "";
// NEXT ASSIGN "checked" TO THE $checkYes OR $checkNo VARAIBLE
// IF ANY OF THE CONDITIONS BELOW IS SATISFIED...
// WE'LL USE THESE VARIABLES BELOW: WITHIN THE RADIO BUTTONS SECTION
if($result_position[$i]->process_resume=="No"){
$checkNo = "checked";
}
if($result_position[$i]->process_resume=="Yes"){
// TO DISABLE ALL RADIO BUTTONS WITH A "YES" VALUE;
// SIMPLE ADD "disabled" TO THE $checkYes STRING LIKE SO:::
$checkYes = "checked disabled";
$checkNo = "disabled";
}
echo '<td id="submit_time">' . $result_position[$i]->submit_time . '</td>';
echo '<td id="last_name">' . $result_position[$i]->last_name . '</td>';
echo '<td id="first_name">' . $result_position[$i]->first_name . '</td>';
echo '<td id="middle_name">' . $result_position[$i]->middle_name . '</td>';
echo '<td id="mobile_number">' . $result_position[$i]->mobile_number . '</td>';
echo '<td id="email">' . $result_position[$i]->email . '</td>';
echo '<td id="location">' . $result_position[$i]->location . '</td>';
echo '<td id="position">' . $result_position[$i]->position . '</td>';
echo '<td id="message">' . $result_position[$i]->message . '</td>';
echo '<td id="resumeFile' . $optionId . '"><a href=' . wp_get_attachment_url($result_position[$i]->attachment_resume_id) . '>Download Resume</a></td>';
echo '<td id="radioOption><label for="Yes">Yes</label>
<input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption_' . $optionId . '" ' . $checkYes . ' value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
<label for="No">No</label>
<input type="radio" id="processedOptionNo'. $optionId.'" name="processedOption_' . $optionId . '" ' . $checkNo . ' value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
Getting the value from MySQL to check radio buttons generates a notice
Your problem is that you have to forgot to put == instead of = as well as you have to put Male in quotes.
It should look like this:
if ($fetch['birthday'] == "Male")
Also, are you sure that birthday field is correct one to check for sex?
Radio Buttons using my fetch array (How to Insert The Values to MySQL?)
First off, if you're not using the ID of the radio buttons for something specific, remove that attribute. A HTML-element doesn't need an ID unless you need to target that specific element.
Secondly, your name
attribute is invalid. In your current set up, you will only get a number as the name. Ex: name='1'
. Or rather name=1'
(you're missing the first ' in your code). Change your code to:
name='questions[". $row ["questionid"] . "]'
This will give you a name like name='question[1]'
When the page is posted, you can get the values like this:
foreach($_POST['questions'] as $questionId => $answer) {
//...insert into your DB.
}
Don't forget to properly escape $questionId
and $answer
before you use them in your DB-queries.
Oh, yes... your values aren't quoted... value=5
should be value='5'
These are the most obvious issues.
...and as others have pointed out, you shouldn't use mysql_*
functions since they are deprecated. Use MySQLi and if you get an error, read the PHP-docs on how to use them.
Changing the radio button value when retrieving from database
<?php foreach($users as $key => $value)
{
// Write Here Like This.
$status=$value['status'] == 1 ?'active':'inactive';
?>
<tr>
<td><?php echo $value['firstname']; ?></td>
<td><?php echo $value['lastname']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $status; ?></td>
<td><?php echo $value['datereg']; ?></td>
<td>
Related Topics
Regex to Check the String Contains Only Letter and Numbers But Not Only Numbers
How to Get Newest Data With 'Created_At' Column
How to Pass a PHP Variable Using the Url
Comparing Date from Variable to MySQL Date
How to Pass Variable Between Two Web Pages With PHP Without Using Session
Getting Error in Console:Failed to Load Resource: Net::Err_Connection_Reset
How to Get All Month Record Count in Laravel
How to Get Previous Month from Selected Month
How to Add Auto Numbering With Each Fetching Row
How to Change PHP Version in Xampp
Convert Svg Image to Png With PHP
Php Json String, Escape Double Quotes for Js Output
How to Enable the Mysqli Extension in PHP 7
How to Convert Text to Svg Paths
How to Fix Undefined Offset: 0 in Laravel Blade
How to Access and Manipulate Multi-dimensional Array by Key Names/Path