combine checkbox values using php mysql - php

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).

Related

Combine form values under one name using GET method

I have a form using the GET method consisting of checkboxes.
This form is sending data to another page that is receiving the GET info and using it to pull info from a json api. I need to have it send the name once with all values combined into one string like this: example.com/color=RedGreenBlue
I am able to get all values combined and echoed onto the page but because they are in a foreach loop I am not able to pass them in the form. I tried below using the hidden field to pass them with no luck.
This is the method I've seen suggested but does not work for me:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
<?php
$name = $_GET['color'];
if (isset($_GET['color'])) {
foreach ($name as $color){
echo $color;
}
}
?>
<input type="hidden" name="MajorArea" value="<?php echo $color; ?>" />
</form>
Is there a way to assign one name to a group of checkboxes? Is there a way to pull the foreach loop data and use it outside a loop? Am I overlooking a way that is much easier than this?
Thanks for any advice!
I'll go out on a limb here and delete if it isn't what you're after. $_GET['color'] will be an array or it will be empty. You could also use isset:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
</form>
<?php
if (!empty($_GET['color'])) {
echo implode($_GET['color']);
}
?>

How to get values of checkbox array in Laravel 5?

I created checkboxes in form using javascript:
<input type="checkbox" name="is_ok[]" />
<input type="checkbox" name="is_ok[]" />
<input type="checkbox" name="is_ok[]" />
When I check 1st and 3rd checkbox and submit the form, Input::get("is_ok") returns me:
['on', 'on']
Is there any way to get value as ['on', null, 'on'] or ['on', 'off', 'on']?
Thanks in advance.
Hey assign some values to checkboxes like user_id, product_id etc what ever in your application.
E.g. View
<input type="checkbox" name="is_ok[]" value="1" />
<input type="checkbox" name="is_ok[]" value="2" />
<input type="checkbox" name="is_ok[]" value="3" />
E.g. Controller
<?php
if(isset($_POST['is_ok'])){
if (is_array($_POST['is_ok'])) {
foreach($_POST['is_ok'] as $value){
echo $value;
}
} else {
$value = $_POST['is_ok'];
echo $value;
}
}
?>
You will get array of selected checkbox.
Hope it helps..
I think I have a "good" solution to this (kind of).
<input type="checkbox" name="is_ok[0]" />
<input type="checkbox" name="is_ok[1]" />
<input type="checkbox" name="is_ok[2]" />
(Forced indices here)
In the request:
$array = \Request::get("is_ok") + array_fill(0,3,0);
ksort($array);
This will ensure that (a) The checkbox indices are maintained as expected. (b) the gaps are filled when the request is received.
It's sloppy but may work.
IMHO this is the best practice:
In your migration set that db table field to boolean and default 0
$table->boolean->('is_ok')->default(0);
{!! Form::checkbox('is_ok[]', false, isset($model->checkbox) ? : 0) !!}
and if you are not using laravel collective for forms then you can use vanilla php
<input type="checkbox" name="is_ok[]" value="<?php isset($model->checkbox) ? : 0; ?>" />
My solution is this for laravel 5
$request->get('is_ok[]');
Though it might not be best practice, here is what I did first of all I send id of a specific model as a value.
<input type="checkbox" id="verify" name="is_varified[]" value="{{$bank->id}}" {{$bank->is_varified == 1 ? 'checked':''}}>
And in controller I added two query to update the field.
//handaling the issue of checkbox
Bank::where("user_id",$user->id)->whereIn('id',$request->is_varified)->update(['is_varified'=> 1]);
Bank::where("user_id",$user->id)->whereNotIn('id',$request->is_varified)->update(['is_varified'=> 0]);

checkbox array only returning one result

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

PHP storing multiple checkbox in database

i did my 'homework' in searching for a solution to this matter through this forum and googling, and ive tried out various ways in solving this, but i can't seem to get the value to be posted into the action file...
i understand the concept of implode / concatenating the array into a string so that it can be stored in the database, i also understand how data are parsed, but i can't seem to get this done..
PHP Form:
<form name="questionnaire_1" action="test_index.php?action=send_questionnaire" method="post">
<table>
<tr class="row-a">
<td>
2. Position<br />
<i>( You may choose more than one option for this question)</i>
</td>
<td colspan="6">
<label> <input type="checkbox" name="position[]" value="1" /> Project Leader (External) </label><br />
<label><input type="checkbox" name="position[]" value="2" /> Project Leader (Internal)
</label><br />
<label><input type="checkbox" name="position[]" value="3" />Project Member (External)
</label> <br />
<label><input type="checkbox" name="position[]" value="4" /> Project Member (Internal)
</label><br />
<label> <input type="checkbox" name="position[]" value="5" />Others (Please Specify)</label>
<input name="position_specify" type="text" size="40" maxlength="40" />
</td>
</tr>
</table>
PHP Action:
if (isset($_POST['Submit']))
{
$position = mysql_prep($_POST['position']);
$allPosition = implode (",", $position);
}
etc ...
the position_specify input has no problem... only having problem with the checkbox... the database takes in varchar(255) for the position.. i really do not know how to solve this..
much help is appreciated, thank you & God bless!
Possible reference to source of mysql_prep() function: http://forums.devnetwork.net/viewtopic.php?f=1&t=81234 . Note that it ends up passing an array to mysql_real_escape_string() which doubtless converts the array to the string "Array", which is then stored in the database. As miki posted in the comments above, imploding before escaping should solve your problem.
You may have a look at bitwise operators. So in otherwords five the first checkbox the value 1 then rhe second value 2 and 3rd value 4. So count in bits. So when submitting the form add all the values together by adding all the values of the checked checkboxes. And then store ome value in the db.
To check if the checkbox was ticked do the following
If($box & 1){ echo 'checkbox 1 is checked';}

How to Store Check box in MYsql?

Am Newbie.I have this coding...How to store into Mysql and How to Retrieve from the Database?
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
am doubt with if i select both 2 box means what will happen?
Please Explain...Thanks in Advance..
If you have no problem with comma separated values
my answer will help you
simple use array name to your field
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
then implode it with comma
implode(","$_POST['like']);
if two box is checked then
result will be yes,no
make your column in varchar
Then do usual stuff to store
yourscript.php:
<?php
include("mysql_connect.php");
if ($_POST['like'])
{
if ($_POST['like'] != "yes" and $_POST['like'] != "no") die("Hacker");
mysql_query("INSERT INTO likes ('id', 'like') VALUES ('', '".$_POST['like']."')");
}
?>
<form method="post" action="yourscript.php">
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
</form>
retreive.php:
<?php
include("mysql_connect.php");
$likes = mysql_query("SELECT * FROM likes");
while($like = mysql_Fetch_assoc($likes))
{
echo "ID ".$like['id']." => ".$like['like'];
}
If you select both fields, it will save the latest select.
You want radio (probably)
You probably don't want to be doing this in the first place. What you really want to be using is radio buttons for yes/no values. Otherwise, you're going to run into problems.
you can use like this
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
to store in db use serialize(); function.
unserialize() return the same array with keys.

Categories