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¤cy=cad
and then $_GET['currency'] returns only one value.
adding name=currency[] just gets /?currency[]=usd¤cy[]=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
Related
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
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">
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']);
I have built a form that has a checkbox input array (saving to an array). However, when I POST it and retrieve the results, it only offers the last selection.
<input type="checkbox" value="Friendly" name="quest[9]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[9]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[9]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[9]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[9]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[9]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
For example, say I chose "Friendly", "Efficient", and "Genuine".
When I POST it over to a PHP document and run
print_r($_POST['quest']);
I only receive
Array ( [9] => Genuine )
back so "Genuine" is the only item I get back. Is there a way to fix this? What have I done wrong?
This is the 9th question on the survey, so changing the name unfortunately is not an option. Is there any way to combine the results to that single array separated by commas? I could always explode on the php side.
All your checkboxes have the same name, make them unique
<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
or use empty square brackets so php will treat the inputs as an array
<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>
use quest[] in name instead of quest[9].
also in php part use this to add multiple choices .
<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>
Happy coding!!
I'm posting a new answer about your comments on the previous one:
Since you must keep quest[9] as the organization for the checkbox array..
You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.
And find the contents by
print_r($_POST['quest']);
again
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).