using radio buttones as check box - php

I am learning how to develop WordPress plugins from this tutorial series and now I am on this tutorial , but I found a strange thing: there is used two RadioButtones to get values "True" or "False" where they could use one CheckBox
<label for="devloungeHeader_yes">
<input type="radio" id="devloungeHeader_yes" name="devloungeHeader" value="true" <?php if ($devOptions['show_header'] == "true") { _e('checked="checked"', "DevloungePluginSeries"); }?> />
Yes
</label>
<label for="devloungeHeader_no">
<input type="radio" id="devloungeHeader_no" name="devloungeHeader" value="false" <?php if ($devOptions['show_header'] == "false") { _e('checked="checked"', "DevloungePluginSeries"); }?>/>
No
</label>
I saw something similar on joomla admin page, where there was used 2 radio buttons to get the same result.
So, is there any (html, css, javascript, php, just any) advantage of using two radio buttons instead of one checkbox?

Actually I found out why using two radio buttons with same names and different values "True" and "False" is better than checkboxes. when a checkbox is NOT checked, it will not be submitted to the server. But when you use radio buttons with "True" and "False" values those radio buttons will be submitted on the server, so php will get result from html form anyway. So with ckeckbox you have only two options:
1) when chebox is checked and is submited (value == "checked")
2) when checkbox is Not checked and it is not submited
with php you have those two options:
<?php
if (isset($_POST['testCheckBox'])) {
// it is submited so it means it is Checked!
} else {
// it is not submited because it is not checked or there is something wrong!
}
?>
But with radio buttons you have three options:
1) when radio "True" is checked and is submited
2) when radio "False" is checked and is submited
3) when radio is not SUBMITED, so something is not working properly
php example:
<?php
if (isset($_POST['testRadioButton'])) { // testRadioButton is submited!
if($_POST['testRadioButton'] == "true"){
// True is checked, do something
} elseif(($_POST['testRadioButton'] == "false"){
// False is checked, do something
}
} else {
// it not Submited! so there is definitely something wrong with code
}
?>

Related

PHP checkboxes that return display and return "Yes" from a database

I am building a site with a number of independant check boxes that collect information about industry topics. I user will check the ones of interest and store “Yes” in the database which is Filemaker.
When they return, they will see the ones previously checked and can uncheck if needed.
To this end, I am trying to get a check box to display as checked if the database value is equal to “Yes” and display as unchecked if the value is blank. Also, of the user checks the checkbox on the form, it will send the value of “Yes” back to the database and a value of blank if the check box is unchecked.
So far, I am only able to display the “Yes” or blank for fields. Here is my code so far:
<input type="text" name="Core_Compentencies__Marketing" value="<?php echo $port_row->getField('Core_Compentencies::Marketing'); ?>"></td>
Any help is appreciated.
Thanks.
usually i use helper function to decide whether field value is checked or not.
<?php
function isChecked($value = '', $defaultVal = 'Yes')
{
if($value == $defaultVal)
{
return 'checked';
}
}
?>
<input name="checkbox" type="checkbox" value="Yes" <?php echo isChecked(Core_Compentencies::Marketing); ?>>

Getting Checkbox values when user edits form data. PHP

I have a form with checkboxes that users fill out and submit. I then have another form that allows them to edit the information they submitted. This second form pre-populates with the information they submitted on the first form. This is fine with text fields, but how do I pre-populate the checkboxes? I.e. if they checked a checkbox on the first form, how do I get the second form to recognise that and display a checked checkbox?
I'm new to Php so sorry I can't be more technical with this query!
Thanks
Luke
When you submit the form, you have the data on the next page. So if theres a checkbox looking like this:
<input type="checkbox" name="over18" value="1" />
You have to check if the person selected the "over 18"-field.
if ($_POST['over18'] == '1') {
$checked = 'checked="checked"';
}
else {
$checked = ''; //If it's not checked
}
Then your output for the checkbox looks like this:
echo '<input type="checkbox" name="over18" value="1" ' . $is_checked .'/>';
So everytime the Checkbox is checked, the next page checks the Checkbox too.

How to make a radio button return boolean true/false instead of on/off

I want that my radio buttons return me a Boolean value true or false instade of on/off
So I pass the true/false in the value of the input :
<label>Male
<input type="radio" name="IsMale" value="true" />
</label>
<label>Female
<input type="radio" name="IsMale" value="false" />
</label>
but it returns me a true/false in a text format. Please masters how could I get them in a booleen format ?
More details : In fact I need to store my $_POST array in a file.txt, and for my radio button I need to store for example :
array ( "IsMale" => true );
and not :
array ( "IsMale" => "true" );
You'll have to check the data and modify it.
if(isset($_POST['SubmitButton'])) {
$_POST['IsMale'] = $_POST['IsMale'] == 'true' ? true : false;
}
You cannot make radio buttons or any other form element directly submit a PHP true value, only a string such as "true".
To solve your problem, you would have to change the value of the $_POST item in your PHP file.
//Form has been submitted
if(isset($_POST['submit'])) {
//Radio button has been set to "true"
if(isset($_POST['IsMale']) && $_POST['IsMale'] == 'true') $_POST['IsMale'] = TRUE;
//Radio button has been set to "false" or a value was not selected
else $_POST['IsMale'] = FALSE;
}
Edit: Ben has provided a functional solution using ternary operators which is a shorter alternative. The example above may clarify exactly what is going on in the process (in a more verbose form).

retaining values of radio buttons

I am a php programmer .I have an image rating page ,in which it displays the image gallary with radio buttons once selected and submitted ,the image button values go to the table .when I retrieve the images they are rendered properly , however they do not show the previous radio button rating selection.
My question is how to retain the most recent radio selections , when images are fetched into the browser.
I'm not sure what your options are but it would be something like this;
<input type="radio" name="image_option" value="1"<?php $_POST['image_option'] == 1 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="2"<?php $_POST['image_option'] == 2 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="3"<?php $_POST['image_option'] == 3 ? ' checked="checked"'; ?> />
Hope that helps.
You'll have to set the "checked" attribute for the appropriate radio buttons that you want to be selected.
if you want to default select then use "Checked".
and if you want to checked on resent selective button. then on submit time, store radio button value in session and when browser get request after submission radio value then retrieve session value and apply checked on previous select radio button.
Your problem is that you need:
The value in the database to match
The value of the radio button itself
Which will allow you to:
Compare the database value (DBV) with the radio button value (RBV) and set it as checked if the comparison comes back as true.
The variables below are:
$ar_rvbs = the array of radioset values (strings or booleans, usually) you are going to loop through and check against the stored DBV.
$value = the value of each item in the radioset value array
$attrval = the stored value for radio button. it doesn't necessarily have to be in a database. you could use the post method to pass it from one page to the next.
$checked = if the DBV matches the RBV when the loop goes through, this will be set to the string "checked" otherwise it is just an empty string.
function makeRadioSet($ar_rvbs,$attrval=[DBV] /*A*/
{
foreach ($ar_rvbs as $value)
{
$checked = ''; /*B*/
if ($attrval==$value) /*C*/
$checked = "checked"; /*D*/
echo '<input type="radio" name = "fieldname" value = "'.$value.'" '.$checked.'>';
}
}
/A/ Pass the list of RBVs as an array and the DBV as a variable
/B/ Set checked to an empty string because that will be the default for all the radio buttons in the set except the one that matches the DBV
/C/ Compare the DBV to the current RBV being processed by the loop from the RBV array
/D/ If the comparison from step C returns true, make the checked string available for insertion into the input element
The echo takes care of generating each radio set option and making sure the one that matches the DBV has "checked" in the input element tag
You can use in following method
#{
string MaleChecked = "";
string FemaleChecked = "";
if(#Model.Gender=="Male")
{
MaleChecked = "Checked";
}
else
{
FemaleChecked = "Checked";
}
}
if form use
Male<input type="radio" name="Gender" id="rdoGender" value="Male" #MaleChecked>
Female<input type="radio" name="Gender" id="rdoGender1" value="Female" #FemaleChecked>
after reload the page you have to add the attribute checked to the radio buttons you have to check.
The correct syntax is:
<input type="radio" name="foo" id="bar" value="1" checked="checked" />

PHP function problem

I have a php function script that is supposed to uncheck a checkbox or checkboxes if a user unchecks it when the preview button is clicked but I can only get the last
checkbox that was unchecked to stay unchecked but not the other checkeboxes how can I fix this so that all the checkboxes that where unchecked stay unchecked?
Here is part of my PHP function that is giving me the problem.
if(isset($_POST['preview'])){
foreach($query_cat_id as $qci) {
if(!in_array($qci, $cat_id)){
$unchecked = $purifier->purify(strip_tags($qci));
}
}
}
for ($x = 0; $x < count($query_cat_id); $x++){
if(($query_cat_id[$x] == $cat['id']) && ($cat['id'] != $delete_id) && ($cat['id'] != $unchecked)){
echo 'checked="checked"';
}
}
Why not just just check if the variable is set, if the checkbox is checked when the form is submitted, it will be available in $_POST['checkboxName'], otherwise, isset($_POST['checkboxName']) will return false.
Basic script to test it
<?php
if (isset($_POST['heh']))
echo $_POST['heh'];
else
echo "Not checked";
?>
<form action='yourPage.php' method='post'>
<input type='checkbox' name='heh' />
<input type='submit' />
</form>
View it in action
http://robertsquared.com/so.php
i solve this problem on client side
i have a input of type hidden with the same name as the checkbox with the value of 0 and the checkbox right after the hidden field with the value of 1
if the checkbox is not checked i get the value of 0 from the hidden field and if someone checks i got the 1 from the checkbox.
so i only need to check if $value==1 then checked=checked

Categories