Identifying if no radio buttons selected with PHP - php

I'm trying to write some code that will determine if no radio buttons are selected on a form
the form consists of many fields but have just included the radio buttons in form below
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
then in the myPHPPage.php page I have something like below to assign the POST value to a variable:
if(!isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
}
But I want some code like: If no radio buttons selected $var = $someValue

You need to bind all-radio buttons in a group like
Value 1 <input type="radio" name="basic" value="basic">
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">
Then in PHP
<?php
if(isset($_POST['basic'])) { //remove ! from condition
$var = $_POST['basic'];};
echo $someValue = $var;
}
?>

TRY THIS....you should have all radio button same NAME
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>
here your php code on server after the submit is press
<?php
if(isset($_POST['send'])) { //change index to submit button name
if($_POST['basic'] == ''){ // if no button is selected then
$var = $someValue;
}//if ends here
else
{ //if any of the radio is selected "if you don't want else so remove that"
$var = $_POST['basic'];
}//else ends here
}//isset ends here
?>

Try this:
<?php
// Handle Post
if (isset($_POST['send']))
{
// Get Post Values
$basic = isset($_POST['basic']) ? $_POST['basic'] : '';
$silver = isset($_POST['silver']) ? $_POST['silver'] : '';
$gold = isset($_POST['gold']) ? $_POST['gold'] : '';
// Atleast one is selected
if (!empty($basic) || !empty($silver) || !empty($gold))
{
echo 'You have made atleast one selection';
}
else
{
echo 'You have not made any selection.';
}
}
?>
<form action="" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>

If you are using radio, all the names of input radio for that particular option should be same.
Value 1 <input type="radio" name="basic" value="basic">
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">
<?php
if(isset($_POST['basic']))
{
$var = $_POST['basic'];
}
?>

The first thing you have to know about radio button is that if they are reflecting the same attribute, they should have a same name.
Thus your form have to be changed like this.
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="radioName" value="basic">
Value 2 <input type="radio" name="radioName" value="silver">
Value 3 <input type="radio" name="radioName" value="gold">
<input type="submit" name="send" value="Submit">
Then you want to assign a value if no radio button is selected.
You can do the following:
$var="";
$someValue="abc";
if(isset($_POST['radionName'])) {
$var = $_POST['radionName'];// This means one radio button is selected, thus you have a value.
}
else{
$var=$someValue; // No radio button is selected, thus you can assign a default desired value.
}

<form action="myPHPPage.php" method="post">
if buttons are optional please make a group of radio button with name attribute value.
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
Then use a php condition here.
<?php
if(isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
}

You can check all $_POST elements,than can compare name and their values.
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
PHP: Possible to automatically get all POSTed data?

<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
<?php
if(!isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
Please Try this one

Related

to checked whether one of the radio button is checked or not from a particular radio group

All the inputs have the same name as option<?=$x?> for each loop but always the input with type=hidden of the last line overrides all of them although one of the radio buttons with same name option<?=$x?> is selected
Is there anyway so that I can check whether anyone out of four radios of above is chosen or not, if not then I have to pass the last input instead of them
<input type="radio" name="option<?=$x?>" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option<?=$x?>" value="null;<?=$data['id'].';'.$data['subject']?>">
You can try this
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option[<?=$x?>]['hidden']" value="null;<?=$data['id'].';'.$data['subject']?>">`
Now when you post you will get option array
$options = $_POST['option'];
$radioValue = $options[$x]['radio'];
$hiddenValue = $options[$x]['hidden'];
You can read value like this
$optionValue = isset($options[$x]['radio']) ? $options[$x]['radio'] : $options[$x]['hidden'];
Hope it may help you

Using the radio button 'checked' value in HTML and PHP

So, I have a questions regarding the input type radio in HTML. As you know, you can put checked as a value, this will mark it as checked.
The story is I am getting a 0 or 1 value from my database. I am then checking if it's 0 or 1 and will then mark one of the radio button's as checked.
My code is as follows:
<?php if($pay_op == 0) { ?>
<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } elseif ($pay_op == 1) { ?>
<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>
My problem now is, whenever I try to mark the other radio button as checked by clicking on it, both radio buttons are checked?
I thought that this might have something to do with me checking if the value returned from the database is 0 or 1 and it will keep one of the radio buttons checked until that value is changed. Now my question is, does anyone know a solution to this issue so that whenever someone clicks on something different than the default checked radio button it will actually check that one and not both of them?
Any tips are highly appreciated! =)
Thanks!
Radio buttons work basically as a named group. The browser only un-checks a radio button if it is linked to the other radio buttons with a property called name.
<?php
if($pay_op == 0)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
elseif($pay_op == 1)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
?>

Validating dynamic radio button group

I am trying to validate the values of a radio button group in php.
The radio buttons are dynamically created in the form.
I can validate the radio button if it is only one radio group, for example.
<form>
<input type="radio" name="radio1">
<input type="radio" name="radio1">
</form>
this is for passing values of radio button
if(isset($_POST['radio1']))
{
*some codes
}
Since the radio buttons in my form are dynamically created, the names of the radio groups increment like radio1, radio2, radio3 so on.
How can I make validation for this dynamic radio button group?
Better create radio button with name as array. Like
<form method="post">
<input type="radio" name="radio[1]">
<input type="radio" name="radio[2]">
</form>
and server side you can check with a foreach
foreach($_POST['radio'] as $key=>$radio){
if($radio == "on"){
echo "$key is checked";
}
}
Try this
<form>
<input type="radio" class="rdo" name="radio[]">
<input type="radio" class="rdo" name="radio[]">
.....
<input type="radio" class="rdo" name="radio[]">//n value
</form>
var arr = new Array();
$('.rdo:checked').each(function() {
arr.push($(this).val());
});
In server side
$i=0;
if(count($_POST['radio'])==0){
return false;
}
foreach($_POST['radio'] AS $rs){
if($rs!=''){
//Some code
}
else{
$i++;
}
}
if($i==count($_POST['radio'])){
return false;
}else{
//some code
}
You can try using radio elements as array like below:
<form method="post">
<input type="radio" name="radio[0]" value="0.1">
<input type="radio" name="radio[0]" value="0.2">
<input type="radio" name="radio[1]" value="1.1">
<input type="radio" name="radio[1]" value="1.2">
<input type="submit" name="s" value="Submit" />
</form>
and from server try
if(isset($_POST['radio'])){
echo "<pr>";
print_r($_POST);
}

Use Radio Buttons to Increase a value in MySql Database

I am trying to set up a voting process where there are 20 sets of two radio buttons you can select from. Then when a "vote" button is clicked I can collect the values of the selected buttons and add +1 to their respective database cells.
I am new to MySql and am struggling...
The database Table looks like..
ID Park Votes
1 Zion 0
2 Grand Canyon 0
3 Arches 0
4 Canyonlands 0
5 Yosemite 0
6 Yellowstone 0
The HTML form
<form action="/databases/save.php" method="post">
<input type="radio" name="match1" value="1" />Zion
<input type="radio" name="match1" value="1" />Grand Canyon
<input type="radio" name="match2" value="1" />Arches
<input type="radio" name="match2" value="1" />Canyonlands
<input type="radio" name="match3" value="1" />Yosemite
<input type="radio" name="match3" value="1" />Yellowstone
<input type="image" src="/graphics/logo.png" />
</form>
And the save.php file
<?php
if (isset($_POST['match1'])){
$match1 = $_POST['match1'];
mysql_query("INSERT INTO results () VALUES ('1')");
}
?>
I am getting thrown off by how to get the selected radio and apply a +1 to the correct table cell. Thanks in advance
Your HTML is incorrect for radio inputs. If you want the user to select only one park, all inputs must have the same name. Use the value attribute of the radio input to store the ID of the park (from the MySQL table):
<form action="/databases/save.php" method="post">
<input type="radio" name="match" value="1" />Zion
<input type="radio" name="match" value="2" />Grand Canyon
<input type="radio" name="match" value="3" />Arches
<input type="radio" name="match" value="4" />Canyonlands
<input type="radio" name="match" value="5" />Yosemite
<input type="radio" name="match" value="6" />Yellowstone
<input type="image" src="/graphics/logo.png" />
</form>
The selected value for that input will be returned as $_POST['match'] if one item was selected or else $_POST['match'] will not be set at all.
So on your save.php page you can update your table like this (assuming your table is named "parks_table"):
<?php
if (isset($_POST['match'])) {
mysql_query( 'UPDATE `parks_table` SET Votes = Votes + 1 WHERE ID = '.$_POST['match'] );
}
?>
Since you seem to be starting with PHP and MySQL please read: Why shouldn't I use mysql_* functions in PHP?
<button type="submit" name="vote" id="vote" value=" ' . $row['candidate_position'] . ' " class="btn btn-success">CAST VOTE</button>
<?php
$vote = $_POST['vote'];
if (isset($_POST['vote'])) {
mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE position_id='$vote'");
echo "string";
}
mysqli_close($con);
?>

Using radio button to set cookie preferences?

I have these 4 radio buttons in which i am submitting to a validatepreferences.php which is the php code below however i am struggling to understand why when i click submit nothing is going through the if statement therefore not giving me my cookie in which to change images based on user input
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image1.jpg">
<br>
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image2.jpg">
<br>
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image3.jpg">
<br>
<input type="radio" name="radioimage"> No Picture
I think the php code must have an error or my if is not right altough i cannot see it.
<?php
if(isset($_POST['radioimage'])){
$radioimage = $_POST['radioimage'];
if ($radioimage == "0" || $radioimage == "1" || $radioimage == "2" || $radioimage =="3") {
setcookie("image", $radioimage, time()+300);
}
}
?>
You're not giving the radiobuttons values in the form. You have to give them values so you can retrieve those values with $_POST in validatepreferences.php. So the HTML should be:
<input type="radio" name="radioimage" value="1"><img class="prefimage" src="../images/image1.jpg">
<br>
<input type="radio" name="radioimage" value="2"><img class="prefimage" src="../images/image2.jpg">
<br>
<input type="radio" name="radioimage" value="3"><img class="prefimage" src="../images/image3.jpg">
<br>
<input type="radio" name="radioimage" value="4"> No Picture

Categories