php - In edit page show selected radio button

Solution:
Something like this
printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n",
($subscribedrips == 'Yes' ? 'CHECKED' : ''));
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n",
($subscribedrips == 'No' ? 'CHECKED' : ''));
works. It uses the ternary operator to either insert'CHECKED'
or an empty string into the input tag, based on the value of$subscribedrips
.
You could also do in a more verbose manner, for example with switch:
$sel_y = '';
$sel_n = '';
switch($subscribedrips)
{
case 'Yes':
$sel_y = 'CHECKED';
break;
case 'No':
$sel_n = 'CHECKED';
break;
default:
// Neither need to be changed, so we dont even need this branch
break;
}
printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n", $sel_y);
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n", $sel_n);
Personal preference really.
Updated Snippet 1
printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n",
((array_key_exists('subscribedrips', $row) && $row['subscribedrips'] == 'Yes') ? 'CHECKED' : ''));
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n",
((array_key_exists('subscribedrips', $row) && $row['subscribedrips'] == 'No') ? 'CHECKED' : ''));
Updated Snippet 2
$sel_y = '';
$sel_n = '';
if(array_key_exists('subscribedrips', $row))
{
switch($row['subscribedrips'])
{
case 'Yes':
$sel_y = 'CHECKED';
break;
case 'No':
$sel_n = 'CHECKED';
break;
default:
// Neither need to be changed, so we dont even need this branch
break;
}
}
printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n", $sel_y);
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n", $sel_n);
Regarding your last question, the difference between our approaches is pretty simple, but once again (ahh!) its a style choice, both accomplish the same goal, both methods are used in "production" PHP code.
My example builds the entire input tag in PHP and prints it. Valentinas' approach pulls the static text out of the PHP strings and puts it directly into HTML.
For example, the following lines will all result in the same output:
<?php printf("<strong>%s</strong>", $some_string); ?>
<?php echo "<strong>$some_string</strong>"; ?>
<?php echo "<strong>".$some_string."</strong>"; ?>
<strong><?php echo $some_string; ?></strong>
I'm doubtful there is any significant performance difference between the two methods, but there is one cosmetic differences that I'll highlight.
Syntax highlighting - If you use an editor with syntax highlighting, valentinas' approach will allow the syntax highlighter to appropriately highlight the
input
tag and its attributes. Using my approach, the entire string would be highlighted the same. Here is a screenshot showing how notepad++ highlights the two methods.As you can see valentinas' approach results in a more colorful display, which could help identify and track down errors.
There are some subtle differences when it comes to how your code has to be formatted if you want to conditionally print the entire tag, but they're not really worth talking about -- the biggest, in my opinion, is the syntax highlighting.
Answer
Solution:
Could it be missing "" near the value?
if ($subscribedrips == "Yes") {
Answer
Solution:
Maybe something like this:
<?php $subscribedrips = $row['subscribedrips']; ?>
<input type="radio" name="subscribedrips" value="Yes" <?php echo ($subscribedrips == 'Yes') ? "checked" : "" ; ?>/> Yes
<input type="radio" name="subscribedrips" value="No" <?php echo ($subscribedrips == 'No') ? "checked" : "" ; ?>/> No
I like taking markup out of PHP tags - makes it more readable.
Answer
Solution:
all the codes which are presented above will work but they look like big or no.of line are more, may be there is nice way if we use like above i have a better and simple way to show the value of the radio button in the page
code in add form:
<td>Gender</td>
<td><input type="radio" name="emp_gender" id="emp_gender" value="M" />Male
<input type="radio" name="emp_gender" id="emp_gender" value="F" />Female</td>
now if you want to edit the field of gender then you have to bring the previously selected value
to do this my code is
<td>Gender</td>
<td><input type="radio" name="emp_gender" id="emp_gender" value="M" <?php echo ($emp_gender == 'M')? "CHECKED" : " " ?> />Male
<input type="radio" name="emp_gender" id="emp_gender" value="F" <?php echo ($emp_gender == 'F')? "CHECKED" : " " ?> />Female</td>
above bolded lines are important in bringing the value of the radio button to the edit form
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: to enable extensions, verify that they are enabled in your .ini files
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.