$_POST from checkbox with the same name and different values [duplicate] - php

This question already has answers here:
getting multiple checkboxes names/id's with php
(4 answers)
Closed 8 years ago.
I have SQL database which has a table (results) that is gonna save all of the results of my survey.
One of the questions is checkbox (multiple answer), so I made a code that should gather the results of checkbox questions and put its values into the database.
But, something's missing, it overwrites the last value in the array so obviously there is an easy solution which I cannot see. To clarify, I want to store all values of one checkbox question if user checks everything.
HTML FORM
<input type="checkbox" name="checkbox1" value="This is the first value">
<input type="checkbox" name="checkbox1" value="This is the second value">
<input type="checkbox" name="checkbox1" value="This is the third value">
PHP SCRIPT
for($i=1;$i<101;$i++) {
if(isset($_POST['checkbox'.$i])) {
$q[$i] .= $_POST['checkbox'.$i].'; ';
}}
<?php
$db =& JFactory::getDBO();
$query = "INSERT INTO jos_results(question1) VALUES ('$q[1]')";
$db->setQuery($query);
$db->query();
?>

If the name of check box type is same then use array then only it will work.
like here
<input type="checkbox" name="checkbox1[]" value="This is the first value">
<input type="checkbox" name="checkbox1[]" value="This is the second value">
<input type="checkbox" name="checkbox1[]" value="This is the third value">

Related

Add rows in the database table depending on the array [duplicate]

This question already has answers here:
How to get a form input array into a PHP array
(9 answers)
Closed 4 years ago.
Please tell me there is an n-th amount of input. It is always different, maybe 1, maybe 20. The code looks like this:
<form action="/1.php" method="post">
<input type="text" value="1" name="more[1]">
<input type="text" value="2" name="more[2]">
<input type="text" value="3" name="more[3]">
<input type="text" value="4" name="more[4]">
<input type="text" value="5" name="more[5]">
<input type="submit">
</form>
How to add to the database table as many rows as we have input in the form? That is, 5 records should be created in the table. Thank you in advance.
On your Action page Create a loop on your input array
Try this
foreach($_POST['more'] as $value){
$query = mysqli_query('Insert Into TableName Values("",$value)');
}
Hope this idea will help you

submitting multiple ans with singl submit button in mysql

i am trying to make a online quiz/survey using php and mysql!and im working with php for the very first time!
what im trying to do is take Questions and its multiple choices from my Db(quiz and table Questions with Qid, Qtext, Ans1..Ans4 as its 6 columns) and once the user is done with the quiz n presses the Submit button on last Question.. all the answers should be saved in Db(quiz and table answer with Aid, Ans, Qid as its columns)! i searched for related codes but couldnt understand any of them.
i would be grateful if somebody could help.
thanks.
have inputs as an array in your view, like:
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
and on submit ,
$answers = $_POST['answer'];
foreach($answers as $answer)
{
...
}
I can give you a short idea
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
<input type="submit" name="submit">
//if you have query on another page that is in form action...there is no need of isset
<?php
if(isset($_POST['submit']))
{
$ans=$_POST['answer']; //store in a variable, now this is array of your multiple answer
//iterate it by loop, best is foreach becouse it will continue iteration untill the element //found in array,
foreach($ans as $val)
{
mysqli_query($con, "insert into table_name set answer='$val'") or die("query failed");
}
?>

How to $_GET multiple checkbox values? [duplicate]

This question already has answers here:
How to get PHP $_GET array?
(8 answers)
Closed 9 years ago.
<input type="checkbox" name="currency" value="usd"/>
<input type="checkbox" name="currency" value="euro"/>
<input type="checkbox" name="currency" value="cad"/>
Im trying to get currency values through $_GET request, something like /?currency=usd,cad but instead im getting /?currency=usd&currency=cad
and then $_GET['currency'] returns only one value.
adding name=currency[] just gets /?currency[]=usd&currency[]=cad
What is the proper way to get these checkbox values in some sort of array?
HTML:
<input type="checkbox" name="currency[]" value="usd"/>
<input type="checkbox" name="currency[]" value="euro"/>
<input type="checkbox" name="currency[]" value="cad"/>
PHP:
<?php
foreach($_GET['currency'] as $currency){
echo $currency."<br/>";
//or what ever
}
?>
Try this way:
name="currency[]"
We have to tell the serverside that this is a name with multiple value.
$_GET['currency'] have to be an array this way.
In your html, make the checkbox name:
<input type="checkbox" name="currency[]" value="usd"/>
This will add the check values to an array. Your method, each check is overwriting the last in your $_GET

How to get the checkbox values in PHP if they all have the same name [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Adding multiple html checkboxes with the same name to URL using $_GET
I have the following code:
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('foo[]');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>
<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
<input type="checkbox" name="foo[]" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo[]" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo[]" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo[]" value="bar4"> Bar 4<br/>
I just want to ask how will I able to get all the values on the checkboxes that are checked using PHP?
I'm not sure I'm understanding you but here's my impression:
You need to use an input array:
<input type="checkbox" name="foo[]" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo[]" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo[]" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo[]" value="bar4"> Bar 4<br/>
Otherwise you're just assigning the last occurrence that is valid as the posted input.
If you want to process multiple boxes give each one a name ending with "[]" to create an array.
Only the values of the checked boxes will be sent to the php script:
// $_REQUEST['foo'] will return an array of the values
echo join(',', $_REQUEST['foo']);

combine checkbox values using php mysql

I having around 20 check boxes in my form as
<input type="checkbox" name="c1" value="1" />
<input type="checkbox" name="c2" value="2" />
<input type="checkbox" name="c3" value="3" />
<input type="checkbox" name="c4" value="4" />
i would like to these values to a database. I just thought rather than creating 20 fields in my database grab all the values at store in the db as 1,2,3,4 and then when querying to explode the values and display it accordingly using php.
can someone please tell me how am i supposed to concatenate the values 1,2,3,4 from the check fields when submitted so i can pass to the database.
i would appreciate if anyone can suggest a different effective way to achieve this using php.
You can change the name of the checkboxes to be the same, something like
<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
Then access them via $_GET or $_POST via
if (isset($_POST['cb'])) {
$my_values = implode(",", $_POST['cb']);
}
If you want them sorted, then you will want to do something like this:
if (isset($_POST['cb'])) {
$my_values = $_POST['cb'];
sort($my_values);
$my_db_value = implode(',', $my_values);
}
For the record, I agree with #Shef in the case that the database can handle the extra load. Depending on when this information will be needed in a highly scalable solution, however, this is a perfectly acceptable way to handle this.
To answer your initial question, first off you need to name your checkboxes all the same name, so:
<input type="checkbox" name="box[]" value="1" />
....
<input type="checkbox" name="box[]" value="20" />
PHP script:
$boxes = $_POST['box'];
$values = implode(",", $boxes); // $values now has "1,2,3,4...", insert into table
A better way would be to have a separate table (see #Shef 's comment).

Categories