Multiple choice from database without <select> - php

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>

Related

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.

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

How can i pass multiple radio button values to MYSQL Database via PHP as all individual values but keeping them segregated into groups?

I have a long questionnaire (46 questions) with each having (3) possible answers, yes, sometimes, and no, (3 radio buttons) each of which have a corresponding value, 10, 5, and -10 respectively.
I had the questions divided up amongst 4 headings with several questions under each heading.
I was able to post all the data to mySQL Database by giving each a different name attribute that corresponded with its field in the Database.
The problem I am having is that I can no longer use the radio buttons as a set, that is, they are all selectable, defeating the purpose of having them, essentially making them check-boxes....
I need to have each radio button its own value in my database and I need them to be grouped in their 3 possible choices for each question...
I will post an example of my form html and php send script....
Thank you in advance for any advice...
html code one question
<div class="questionBox">
<p>1.Have I clearly defined my companies target market?</p>
<input type="radio" name="ca1y" value="10"/>
<label for="ca1">YES</label>
<input type="radio" name="ca1s" value="5"/>
<label for="ca1">SOMEWHAT</label>
<input type="radio" name="ca1n" value="-10"/>
<label for="ca1">NO</label>
</div>
/* php post update */
$radioPost="INSERT INTO questions
( ca1y,
ca1s,
ca1n,...)
VALUES
( '$_POST[ca1y]',
'$_POST[ca1s]',
'$_POST[ca1n]',...)
I tried to give each radio button an ID attribute and use that to update the database but I can't seem to get that to work....
any help would be greatly appreciated
cheers.
I'm not sure if I'm getting the jist of what you trying to do, but it seems like you need to save an individuals answers to different questions to the DB but that each question can only have ONE of let's say three possible answers. Well, in that case you might think of redesigning your DB to look something like this:
You store the users details in the users table, the question details in the questions table and the different answers in the answers table. A user will be able to answer multiple questions but can only answer a particular question once. You then save the answer in ONE field called answer_value for example. There is no need to store the answer in multiple fields since you will be storing the VALUE of the radio button they chose, i.e you will store either a 10, 5 or -10 depending on which radio option was selected.
It's also important that in your HTML you name all the radio buttons for a particular question the same. This ensures that the radio options act as radio buttons and not as checkboxes. Your HTML would most likely look something like this:
<div class="questionBox">
<p>1.Have I clearly defined my companies target market?</p>
<input type="radio" name="q1" value="10"/>
<label for="ca1">YES</label>
<input type="radio" name="q1" value="5"/>
<label for="ca1">SOMEWHAT</label>
<input type="radio" name="q1" value="-10"/>
<label for="ca1">NO</label>
</div>
<div class="questionBox">
<p>2.Have I clearly defined my companies product?</p>
<input type="radio" name="q2" value="10"/>
<label for="ca2">YES</label>
<input type="radio" name="q2" value="5"/>
<label for="ca2">SOMEWHAT</label>
<input type="radio" name="q2" value="-10"/>
<label for="ca2">NO</label>
</div>
.....
Note that question 1 has 3 radio options all named "q1" and question 2 has 3 radio options all named "q2". Follow the same convention for all your questions. Your PHP then becomes pretty straightforward and will probably look something like this:
$radioPost="INSERT INTO answers (user_name, question_name, answer_value)
VALUES (('$_POST[user_name]', 'q1', '$_POST[q1]),('$_POST[user_name]', 'q2', '$_POST[q2]), ....)
If you not sure what the radio button names are, you can use a foreach loop as follows:
$user = $_POST[user_name];
foreach ( $_POST as $key => $val )
{
If ($key <> 'user_name') //you might need to check that you only reading the radio options
{
$radioPost="INSERT INTO answers (user_name, question_name, answer_value)
VALUES ('$user', $key, '$val')"
....
}
}
Also be advised that even if you only using radio options, you should still clean the inputs to prevent attacks like SQL-Injection etc. I suggest you find some more info on securing your code against attack.
BTW, if you place the questions in a table of it's own, you can dynamically create your questionnaire from this table, which then allows you to add or delete questions at will, making your whole application a lot more flexible. And saving your answers in one field instead of three, will remove all the NULL values from your table, which will simplify your queries when you perform calculations. I know MySQL normally puts a zero as the default integer value, but it's still better to save it into one field. If you want to know which option a user chose for a particular question, just check the value stored in that field.
Not very good solution: write js to deselect appropriate radio buttons.
On more serious note i don't understand the need to have all radios with different names. Why is such solution not applicable: name radios in one question with same name, lets say question1, then after form post just look at value and from that you can easily update db. I suppose you want to collect how many times each radio was selected. So if you know that question1 had choice 1 you than also know that question1 did not have choices 2 and 3. Unless you collect data in other way or for other purpose, but then I think there need to be a bit more input from you about the purpose of this. Though I still think you can do this like i said, this would not need complex processing, id say rudimentary programming knowledge should suffice.

Insert values from checkboxes in various tables?

wich contains a list of students on the left and a lot of skills to assign to each student.
Each student have it´s own table with the full skill list (HTML, CSS, ...). Now whats a simple way to get value from the checkboxes and assign them to the corresponding student table?
The purpose is to creating a graph for each student showing their skillset :-) ah, sorry for my poor english.
Print the form using a scripting language like PHP. Use a smart naming for checkboxes, then put the logic to retrieve data in the script that receive the post form.
If you use php, u can use
<form>
<input type="checkbox" name="paul[skill1]" value=1>
<input type="checkbox" name="paul[skill2]" value=1>
...
<input type="checkbox" name="robert[skill1]" value=1>
<input type="checkbox" name="robert[skill2]" value=1>
...
</form>
when submitting the page, the $_POST result will be
$POST = array(
[paul] = array( [skill1]=> 1, [skill3]=>1) //paul has skills 1 and 3
[robert] = array( [skill1]=> 1, [skill4]=>1) //paul has skills 1 and 4
)
if you want to do it everything on the frontend, you should use JQuery, but than managing data it's more difficult.
I suppose you have PHP skills, otherwise use a Spreadsheet to do this

Categories