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);
}
Related
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
}
?>
I have a form that did submit 1 out of 8 radio buttons to a php $_POST super global array. Therefore, I needed some validation. I was kindly helped by being provided this code which works great:
$("#form").submit(function (event) {
if(!$(":radio:checked").length) {
alert("You must select at least one emotional state!");
event.preventDefault();
}
});
However, I have now been asked to have 2 sets of 8 radio buttons in which the user selects 2 answers instead of the initial 1 answer. I need the code to be able to determine that at least one radio button from each set of 8 buttons has been selected before the form is submitted. At the moment the code checks to see if any radio buttons have been selected, then as soon as just 1 buttons is selected, the function is satisfied and transitions to the next page, which is not what I want.
EDIT
Buttons code:
<p><input type="radio" value="happy" name="perceived_emotion">Happy
<input type="radio" value="excited" name="perceived_emotion">Excited
<input type="radio" value="angry" name="perceived_emotion">Angry
<input type="radio" value="frustrated" name="perceived_emotion">Frustrated
<input type="radio" value="miserable" name="perceived_emotion">Miserable
<input type="radio" value="sad" name="perceived_emotion">Sad
<input type="radio" value="tired" name="perceived_emotion">Tired
<input type="radio" value="relaxed" name="perceived_emotion">Relaxed</p>
<p><input type="radio" value="happy" name="induced_emotion">Happy
<input type="radio" value="excited" name="induced_emotion">Excited
<input type="radio" value="angry" name="induced_emotion">Angry
<input type="radio" value="frustrated" name="induced_emotion">Frustrated
<input type="radio" value="miserable" name="induced_emotion">Miserable
<input type="radio" value="sad" name="induced_emotion">Sad
<input type="radio" value="tired" name="induced_emotion">Tired
<input type="radio" value="relaxed" name="induced_emotion">Relaxed</p>
Here is the form code:
<form id="form" action="audio_handler.php?id=1" method="POST">
<div id="perceived_emotions">
<?php include("includes/induced_emotion_buttons.php"); ?>
</div>
<br />
<div id="induced_emotions">
<?php include("includes/perceived_emotion_buttons.php"); ?>
</div>
<p class="right"><input type="submit" name="submit" value="Submit"></p>
</form>
What you want to do is use the [name="value"] selector in JQuery in conjunction with the :checked selector. So your new code would be:
if(!$('input[name="perceived_emotion"]:checked').length) {
alert("You must select at least one perceived emotional state!");
event.preventDefault();
return false;
}
if(!$('input[name="induced_emotion"]:checked').length) {
alert("You must select at least one induced emotional state!");
event.preventDefault();
return false;
}
All of that wrapped into the form event.
EDIT: Since you only want to display one dialog at once, just add return false; in each if statement.
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
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
}
}
I have an HTML page that contains two radio buttons ("yes" and "no") along with a submit button.
How can I specify that I want to execute a PostgreSQL query ONLY if the submit button is pressed AND the "yes" radio button is selected?
Your radio buttons need to have the same name, so check the value that is submitted for that field:
if ($_POST['radio'] == 'Yes')
{
// Execute query
}
Given HTML like this
<form action="foo.php" method="post">
<label for="yesno_yes">
<input type="radio" name="yesno" value="yes" id="yesno_yes"> Yes
</label>
<label for="yesno_no">
<input type="radio" name="yesno" value="no" id="yesno_no"> No
</label>
<input type="submit" name="submit_btn" value="Submit">
</form>
You can check using this
<?php
if (isset($_POST['submit_btn'], $_POST['yesno']) && $_POST['yesno'] == 'yes') {
// do stuff here
}