65162

Assigning a value to a radio, select or checkbox input with PHP

Question:

I've got a registration form. When users submit the form after entering an invalid coupon code, the page reloads and I show the form again with all the data they entered. It's easy with input fields but I can't think of a simple way to do the same with radio, checkboxes and selects.

For example I have :

<input type="radio" name="gender" value="male" /> <input type="radio" name="gender" value="female" />

I have my PHP variable $gender that contains 'male'. But how to have the radio with 'male' selected?

Same question with my selects and checkboxes.

Answer1:

For a small number of options you could use a ternary operator like this.

<input type="radio" name="gender" value="male" <?php echo ($gender == 'male') : 'selected="selected"' : '';?> /> <input type="radio" name="gender" value="female" <?php echo ($gender == 'female') : 'selected="selected"' : '';?> />

Not the most subtle way but it will work. For more complicated situations its advisable to use a DOM parser like <a href="http://php.net/manual/en/class.domdocument.php" rel="nofollow">DOMDocument</a> etc.

Answer2:

//retrive gender and checkbox value in edit form $gender=$row['gender']; $chkhobby=$row['chkhobby']; <tr> <th>GENDER</th> <td> Male<input type="radio" name="gender" value="1" <?php echo ($gender== '1') ? "checked" : "" ; ?>/> Female<input type="radio" name="gender" value="0" <?php echo ($gender== '0') ? "checked" : "" ; ?>/> </td> </tr> <tr> <th>Hobbies</th> <td> <pre><?php //print_r($row); $hby = @explode(",",$row['chkhobby']); //print_r($hby); ?></pre> read<input id="check_1" type="checkbox" name="chkhobby[]" value="read" <?php if(in_array("read",$hby)){?> checked="checked"<?php }?>> write<input id="check_2" type="checkbox" name="chkhobby[]" value="write" <?php if(in_array("write",$hby)){?> checked="checked"<?php }?>> play<input id="check_4" type="checkbox" name="chkhobby[]" value="play" <?php if(in_array("play",$hby)){?> checked="checked"<?php }?>> </td> </tr> //update $gender=$_POST['gender']; $chkhobby = implode(',', $_POST['chkhobby']);

Recommend

  • How to conditionally add widgets to a list?
  • How to include multiple select statements in one table
  • How to use aliases in query for making conditions in mysql
  • get checkbox and radio button value in lift
  • MockMvc: forwardedUrl is null
  • How do I load custom configuration files with codeigniter?
  • How to use Boost Spirit with variant with more than 20 types?
  • Vim syntax highlighting
  • TagHelpers add custom class for LabelTagHelper based on validation attribute [Required]
  • How can we get radio button values from form using body-parser on an expressjs server?
  • PL/SQL: re-write SELECT statement using IN parameter in stored procedure
  • Condition in ternary operator doesn't cause any change
  • What does this PHP (function/construct?) do, and where can I find more documentation on it?
  • Why is this code not working? Hangman
  • JQuery UI selectable plugin - Multiple mouse drag selection and unselect option
  • Combine two small queries (that group by different values) into one query
  • Integrate two requests into one Javascript
  • How to display the hotel names that have the maximum count from data base by calculating sum in PHP?
  • UI-GRID column summation on checkbox change
  • Symfony2.1 form date field: Argument 1 passed to … must be an instance of DateTime
  • FlexJSON Orders Alphabetically by Default
  • SQL - count occurrences of gender
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Set the selected item in dropdownlist in MVC3
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • A cron job substitute?
  • json Serialization in asp
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal