I'm wondering if let's say I have 4 radio buttons named red blue yellow and green but if none of them are unchecked and a person submits the form, let's say will echo no radio checked.
if I use the if statement I can only think of using something like
if(empty($_GET(["red"])) || empty($_GET(["blue"])) || empty($_GET(["green"])) ||empty($_GET(["yellow"])))
{
echo "blah";
}
But that's really long for that if statement if there's two more radio I want to add or something let's say I have a name field an ya....that's gonna be a long long if statement...
is there a way to group them together and make them shorter?
Radio buttons are grouped by default if you give them the same name
<input type="radio" name="group" value="1"> Option 1
<input type="radio" name="group" value="2"> Option 2
The selected option will then be passed to php in $_POST['group']
Related
I'm trying to make a form with Symfony to be able to select multiple choice by checking checkboxes without having a <select> dropdown alone in my form.
Theses choice come from a table in my database, each row must be a choice possibility in my form.
For example table T_Choices :
ID
Choices
1
Choice_1
2
Choice_2
I'm able to create the form with many checkbox as there are rows.
But how can i submit this "dynamic" form and get thoses data in one "array" in my controller to have for example $form["choices"]->getData()[0] or 1 or 2 etc.
Thank you for your help. And sorry for my bad english.
By giving those checkboxes the same value for the name-attribute and adding square brackets at the ending [].
You probably need:
<input id="1" name="choices[]" type="checkbox" value="1"><label for="1">Choice_1</label>
<input id="2" name="choices[]" type="checkbox" value="2"><label for="2">Choice_2</label>
I have a foreach statement that echos radio buttons.
All those radio buttons have same name.
When I want to get the clicked ones, I use $_POST['radio_name']
But I got an error (it can't find the radio name)
This my code :
<form method="post">
<div class="repas-inside-bloc breakfast-bloc" id="brkID">
<?php
foreach($breakfast_array as $brk){
echo '<label for="'.$brk['id_plat'].'" class="plan-meal-box">'.$brk['titre_plat'].'</label><input name="brk_check" type="radio" id="'.$brk['id_plat'].'" value="'.$brk['titre_plat'].'">';
}
?>
</div>
How to get values of every radio button clicked ? $_POST['brk_check'] doesn't work
The idea of radio buttons is they are grouped by their name attribute. If all radio buttons have same name, only one can be clicked at a time.
Therefore, the server value you receive as $_POST['brk_check'] is value of radio button that was clicked, which can only be one value at any given time.
If you want to receive multiple values, you have to name radio buttons differently. But with this usage, if you want to allow multiple selections, you should probably type='checkbox' instead of radio buttons. Here is an answer of how to read multiple checkbox values.
You can try this:
$_REQUEST['brk_check'];
I have a simple form that should insert radio button value into database:
<form method="post" action="insert.php">
<input type="radio" name="yes" value="1"/>Yes<br />
<input type="radio" name="yes" value="0"/>No
<input type="submit" name="submit" value="Send"/><br />
</form>
However, in the database, there is always inserted only value 1.
Could you please help me with this? Here is insert.php
$var = mysqli_real_escape_string($con, $_POST['yes']);
$sql="INSERT INTO table (value)
VALUES ('$var')";
Kindly thank you for advice.
We Use Radio Buttons To Send One Data Only. If you want to use multiple data, use checkbox.
Checkboxes are mainly use when presenting the user with a number of options, of which, they can select more than one option.
For example, Like courses-> PHP, Java, C#, C, C++. The user can check multiple courses.
But, Radio buttons are generally used when presenting the user with an "either/or" option. The user is presented with two or more options, of which, the user can only select one option. For Example, Like Gender-> Male or Female. The user can only select one.
I am building a survey system. I have a page containing 10 items, each item has yes/no options to select. Something like this:
Yes No
1. o o
2. o o
...
<submit>
When I hit submit button, I want to know which items have been selected to yes and no. How can I do that? Thanks.
I know how to retrieve radio button value by name, my question is how to get buttons whose selected value is "yes" and "no". I kinda know how to do it in javascript, but I am not sure how to pass the javascript value back to the PHP script.
If you have something like this
<input type="radio" value="1'"name="radbut">Yes
<input type="radio" value="2" name="radbut">No
the php to select the selected value is like this
$selected=$_GET["radbut"];
I will go into a detailed explanation.
<input type="radio" name="option1" value="Yes"> Yes
<input type="radio" name="option1" value="No"> No
When you're processing this via PHP, there will only be a single "option1" posted value. It will either be Yes, or No, based on which input the user selected.
echo "The user selected: ".$_POST['option1'];
If yes, the above will return:
The user selected: Yes
Same goes for the No.
I'm really confused about how to start on my PHP with radio buttons and check boxes.
How do I print out the option chosen by the user?
Also, declaring lots of fields with similar name but with different answers.
It also needs to be connected to a mySQL database.
checkboxes:
<input type="checkbox" name="theoptions[]" value="1"> Option 1...
<input type="checkbox" name="theoptions[]" value="2"> Option 2...
<input type="checkbox" name="theoptions[]" value="3"> Option 3..
.
If user will submit checked 2 & 3 then $_REQUEST['theoptions'] will be array( 2, 3 )
To store in mysql you can create table for selected options for concrete user or implode() that array to store somethin like "2,3" or "2|3"
If you want to save an array in MYSQL I use serialize to save it in the mysql http://www.php.net/manual/en/function.serialize.php
and to get the array back then use http://php.net/manual/en/function.unserialize.php
so it would look like something like
$options=$_GET['theoptions']; //never use $_REQUEST
serialize($options);
//save to mysql database