34085

In edit page show selected radio button

Question:

On my edit pages, how would I show the correct radio button if for $subscribedrips is equal to Yes or No? Here is what I have and its not working:

if ($row['subscribedrips'] == Yes) { echo '<input type="radio" name="subscribedrips" value="Yes" CHECKED /> Yes <input type="radio" name="subscribedrips" value="No" /> No'; } elseif ($row['subscribedrips'] == No) { echo '<input type="radio" name="subscribedrips" value="Yes" /> Yes <input type="radio" name="subscribedrips" value="No" CHECKED/> No'; } elseif (empty($row['subscribedrips'])) { echo '<input type="radio" name="subscribedrips" value="Yes" CHECKED/> Yes <input type="radio" name="subscribedrips" value="No" /> No'; }

Answer1:

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.

<hr />

<em><strong>Updated Snippet 1</strong></em>

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' : ''));

<em><strong>Updated Snippet 2</strong></em>

$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); <hr />

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.

<ul><li>

<strong>Syntax highlighting</strong> - 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. <a href="http://i.imgur.com/hnKNv.png" rel="nofollow">Here is a screenshot</a> 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.

</li> </ul>

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.

Answer2:

Could it be missing "" near the value?

if ($subscribedrips == "Yes") {

Answer3:

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.

Answer4:

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

Recommend

  • Translation limit in C
  • RcppArmadillo and C++ division issue
  • psycopg2 fails on execute many statement with syntax error
  • How to find documents with exactly the same array entries as in a query
  • Get pretty git rev name
  • git post-receive hook to update multiple servers
  • Radio button show hover between two radio button using jquery
  • Django Rest Framework: getting lists from query_params without request.getlist
  • Simple command-line app I/O in Dart
  • “git push” doing the same as “git fetch” from the remote
  • How to remote debug with Delve debugger in Visual Studio Code
  • How to distribute Java-based software?
  • Visual studio alerts workspace already exists
  • Why can't I use non-integral types with switch [duplicate]
  • C# - Most efficient way to iterate through multiple arrays/list
  • netsh acl setting (need alternative method - registry settings?)
  • Who propagate bugfixes across branches (corporate development)?
  • Java color detection
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • OOP Javascript - Is “get property” method necessary?
  • Remove final comma from string in vb.net
  • How do I alternate colors in Flat List (React Native)
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Could not find rake using whenever rails
  • Scrapy recursive link crawler
  • Ajax Loaded meta Tags
  • How to rebase a series of branches?
  • Xamarin Forms - UWP Fonts
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How to handle AllServersUnavailable Exception
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • jquery mobile loadPage not working
  • How do I rollback to a specific git commit
  • Unanticipated behavior
  • Arrays break string types in Julia
  • python regex in pyparsing
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?