how to get radio button names based on value - php

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.

Related

Multiple choice from database without <select>

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>

php - Get radio button name attributes value

Sorry if this is a stupid question but im really not sure how to tackle this, perhaps im overthinking. I need to retrieve both the name attributes value and the value attributes value. Have a look at img below:
echo'<input type="radio" name="'.$eventId[].'" value="'.$team1.'">';
The name contains the event_id and the value contains user selection. I need the name Ids value to insert event Id into db along with user selection.
I know how to retrieve the rad value attribute but not sure about the name, maybe Im overthinking it or need to change my logic. Any ideas?
If user select Australia radio button than in PHP you will get value 'Australia' but you want the value 'Australia_80' to store in db. Change all your radio button values and names like
<input type="radio" value="Australia_80" name="getRadio">
<input type="radio" value="Canada_81" name="getRadio">

Radio button inserts only 1 value into SQL database

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.

is there a way to group radio button for php usage?

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']

declaring in PHP application forms w/ radio buttons & check boxes to save

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

Categories