radio button cannot empty - php

This is my code for radio button. I want add condition, when one radio button not selected by user, then it will come out warning there is data empty. When complete, it will count sum.
<form action="" method="post">
Player 1:
male <input type="radio" name="gender[1]" value="1">
female <input type="radio" name="gender[1]" value="2">
<br>
Player 2:
male <input type="radio" name="gender[2]" value="1">
female <input type="radio" name="gender[2]" value="2">
<br>
Player 3:
male <input type="radio" name="gender[3]" value="1">
female <input type="radio" name="gender[3]" value="2">
<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$sum=0;
if (isset($_POST['gender']))
{
$gender=$_POST['gender'];
foreach ($gender as $value)
{
$sum=$sum+$value;
}
echo $sum;
}
else
{
echo 'you did not choose any genders';
}
}
?>

If all radio buttons are selected, count($_POST['gender']) would be 3. So just replace
else
{
echo 'you did not choose any genders';
}
with
if (count($_POST['gender']) < 3) {
echo 'you did not choose any genders';
}

It's simple, you can write
if(!$_POST['gender']) echo ....;
or, if you want to check single positions
if(!$_POST['gender'][0] && !$_POST['gender'][1] && !$_POST['gender'][2]) echo ...;

Related

Identifying if no radio buttons selected with 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

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);
}

Having a different action for each radio button in a form

I was wondering if it is possible to make each radio button in a form link to a different action php page? say if I had 2 radio buttons, one named 'basketball' and one named 'football' would I be able to have them link to different php? here is my code;
<form action="football.php" method="post">
<p>Please select your first Sport:</p>
<input type="radio" name="sport" value="football">Football<br></input>
<input type="radio" name="sport" value="basketball">Basketball<br></input>
<input type="radio" name="sport" value="tennis">Tennis</input>
<br><input type="Submit" name="Submit" value="Submit"></form>
Is there any reason they need to be distinct pages?
form.php
<form action="sport.php" method="post">
<p>Please select your first Sport:</p>
<input type="radio" name="sport" value="football">Football<br></input>
<input type="radio" name="sport" value="basketball">Basketball<br></input>
<input type="radio" name="sport" value="tennis">Tennis</input>
<br><input type="Submit" name="Submit" value="Submit"></form>
sport.php
<?php
if (isset($_POST["sport"])
&& $_POST["sport"] == "football") {
//logic specific for football
} else if (isset($_POST["sport"])
&& $_POST["sport"] == "basketball") {
//logic specific for basketball
} else if (isset($_POST["sport"])
&& $_POST["sport"] == "tennis") {
//logic specific for tennis
} else {
//die or some kind of error handling can be done
}
?>
If they absolutely need to be different pages, you can do something like the below:
sport.php
<?php
echo "<meta http-equiv='refresh' content='0;url=./dir/subdir/".$_POST["sport"].".php'/>";
//so if posted form data == football, redirect to football.php, etc
?>
Sure it lacks finesse, but you won't be able to get your desired outcome otherwise, unless you use jQuery/JS.

Form action based on Input Type selection

I am creating a form in php on the page "index.php". When the page is loaded the following form is created:
if($_SERVER['REQUEST_METHOD']!='POST')
{
echo '
<form action="index.php" method="POST">
<fieldset style="width: 700px;">
<legend>Enter your search below</legend>
<textarea rows="1" cols="80" name="query">
</textarea>
</fieldset>
<p>
<input type="radio" value="Non-Aggregated"> Non-Aggregated
<input type="radio" value="Aggregated"> Aggregated
<input type="submit" value="Search">
</p>
</form>';
}
When the user clicks the submit button, the appropriate content is displayed:
else
{
if ($_POST['query'])
{
//content displayed after form submission
}
}
Going back to the form, note the radio options:
<input type="radio" value="Non-Aggregated"> Non-Aggregated
<input type="radio" value="Aggregated"> Aggregated
Is there a condition that I can place in the if-statement to carry out a different action based on whether Non-Aggregated or Aggregated is selected from the radio buttons; and if so how would I go about doing this?
Thanks for any help at all.
Give name attribute to radio buttons, like
<input type="radio" name="aggr" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="aggr" value="Aggregated"> Aggregated
After POST method is executed you can check values with PHP:
if($_POST['aggr']=='Aggregated'){
//DO STUFF
}
if($_POST['aggr']=='Non-Aggregated'){
//DO OTHER STUFF
}
In other way, you can set names, like
<input type="radio" name="Non-Aggregated" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="Aggregated" value="Aggregated"> Aggregated
And check this if isset
if(isset($_POST['Aggregated'])){
//DO STUFF
}
if(isset($_POST['Non-Aggregated'])){
//DO OTHER STUFF
}
First of all, asign a "name" to your radios. Same to both.
<input type="radio" name="aggregation" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="aggregation" value="Aggregated"> Aggregated
On the "else" clause, make a small change, insted of
if($_SERVER['REQUEST_METHOD']!='POST')
use
if($_SERVER['REQUEST_METHOD']=='POST')
And last, but not least, add your condition inside that last if...
if ($_POST['aggregation'] == 'Aggregated')
{
// Actions for Aggregated
}
else
{
// Actions for Non-Aggregated
}
Here is working code:
<?php if($_SERVER['REQUEST_METHOD']!='POST')
{
$form = <<<FORM
<form action="" method="POST">
<fieldset style="width: 700px;">
<legend>Enter your search below</legend>
<textarea rows="1" cols="80" name="query">
</textarea>
</fieldset>
<p>
<input type="radio" value="Non-Aggregated" name='radioCheck'> Non-Aggregated
<input type="radio" value="Aggregated" name='radioCheck'> Aggregated
<input type="submit" value="Search">
</p>
</form>
FORM;
echo $form;
}
elseif($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['radioCheck']) && $_POST['radioCheck'] =='Non-Aggregated'){
// Non-Aggregated form
echo 'Non-Aggregated';
}elseif($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['radioCheck']) && $_POST['radioCheck'] =='Aggregated'){
// Aggregated form
echo 'Aggregated';
}
?>
<?php
if($_SERVER['REQUEST_METHOD']!='POST')
{
// echo the form and don't forget to give the radio inputs a name
// <input name="agg" type="radio" value="Non-Aggregated"> Non-Aggregated
// <input name="agg" type="radio" value="Aggregated"> Aggregated
}
else if (isset($_POST['agg']))
{
if ($_POST['agg'] == 'Aggregated') {
// do one thing
} else {
// do something else
}
}

How do I see which checkbox is checked?

How do I check in PHP whether a checkbox is checked or not?
If the checkbox is checked, then the checkbox's value will be passed. Otherwise, the field is not passed in the HTTP post.
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}
you can check that by either isset() or empty() (its check explicit isset) weather check box is checked or not
for example
<input type='checkbox' name='Mary' value='2' id='checkbox' />
here you can check by
if (isset($_POST['Mary'])) {
echo "checked!";
}
or
if (!empty($_POST['Mary'])) {
echo "checked!";
}
the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo htmlspecialchars($aDoor[$i] ). " ";
}
}
Try this
index.html
<form action="form.php" method="post">
Do you like stackoverflow?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
form.php
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['like']))
{
echo "<h1>You like Stackoverflow.<h1>";
}
else
{
echo "<h1>You don't like Stackoverflow.</h1>";
}
?>
</body>
</html>
Or this
<?php
if(isset($_POST['like'])) &&
$_POST['like'] == 'Yes')
{
echo "You like Stackoverflow.";
}
else
{
echo "You don't like Stackoverflow.";
}
?>
If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox.
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
This way you will get 1 or 0 based on whether the checkbox is selected or not.
I love short hands so:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";

Categories