PHP storing multiple checkbox in database - php

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';}

Related

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]);

How to read the form's inputs by type?

I have a form:
<form name="form1" method="post" action="form_to_write.php">
<h4>q1</h4>
<input type="radio" name="a1" value="someValue1" />someValue1<br />
<input type="radio" name="a1" value="someValue2" />someValue2<br />
<input type="radio" name="a1" value="someValue3" />someValue3
<h4>q2</h4>
<input type="radio" name="a2" value="someValue4" />someValue4<br />
<input type="radio" name="a2" value="someValue5" />someValue5<br />
<input type="radio" name="a2" value="someValue6" />someValue6
<h4>q3</h4>
<input type="radio" name="a3" value="someValue9" />someValue9<br />
<input type="radio" name="a3" value="someValue7" />someValue7<br />
<input type="radio" name="a3" value="someValue8" />someValue8
<input type="submit" value="submit" name="submit"/>
</form>
And want to read all inputs to array by type (radio). I know, how to read it by name, but how by type?
The input type attribute is not sent to the server when the form is submitted. Only the name and the value are sent. You will need to keep track of what's what yourself on the server using useful names.
make your form_to_write.php like this:
<?php
print_r($_POST);
and study it's output.
It contains everything you can get from the form. You are free to choose what to use. Enjoy.
As your question being a perfect example of a badly asked question, I can only guess your real needs.
It seems you want to get an array contains all radio buttons. You still can do it by using names.
make your radio buttons names like this
<input type="radio" name="radios[a1]" value="someValue1" />someValue1<br />
<input type="radio" name="radios[a2]" value="someValue4" />someValue4<br />
<input type="radio" name="radios[a3]" value="someValue9" />someValue9<br />
and you'll be able to access $_POST['radios'] array which contains all your radio fields
If you are looking for a PHP function like GetAllInputsOfType("radio") then you won't find it (unless you can do somethign fancy with the DOM, like JS does; maybe this will help?).
What I have done in similar circumstances is to rename my input fields according to type, so instead of a1, a2, a3, you could have radio_a1, radio_a1, radio_a3 and text_a4, memo_a5, listbox_a6, etc (and, btw, use some meaningful names, not a1, a2, a3 ;-)
Then you can loop thorough the array $_GET or $_POST looking for elements beginning radio_ ...
You could use something like a Zend_Form which keeps track of it (and could even do validating jobs etc). But you can't get the type of a form field with just php - you'll need to do things in JS which is on the client side and may not be trusted.

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

Unchecked checkboxes in an array

I'm building a form with the possibility to add more group of fields, to process them i read out the array in a for loop
the script:
<?php
foreach ($_POST as $key => $value) {
$$key = $value;
}
$count = count($name);
for ($i=0; $i<$count; $i++){
?>
<strong><?php echo $name[$i]; ?></strong> (<?php echo $check[$i]; ?>)<br /><?php echo $select[$i]; ?><br /><br />
<?php
}
?>
<form method="post">
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<button>Add another group</button>
<input type="submit" />
</form>
If all checkboxes are checked there is no problem but if only the last one is checked it counts only one checkbox in the array, name[0] is then combined with check[0] but check[0] is really check[2]. English is not my native language so i don't know the right words.
Actually, there is a decent workaround for this as proposed by Sam in this answer on Stack Overflow:
Post the checkboxes that are unchecked
It worked for me, and I suspect you and I had a similar problem (mine being that I had/have upwards of 300 input fields in similar(ish) groups and didn't want to write validation rules for every one of those individual fields, just rules targetted at each family of input types e.g. the email addresses, or the postcodes. In brief, the technique is that you place a hidden input field, with the same name, before your checkbox field. Setting the value of the hidden field (type='hidden') to '0' will ensure that at least one key/value appears in your POST array, with the '0' being superceded by a later '1' only if the box is checked. I needed the '0' value to allow people to 'unset' an option they had previously 'set', for example that they were willing to show their contact data. This technique allows me to present the user with much the same form for an update as they would get at at first registration. Thanks to Sam!
That's normal PHP behaviour, when a checkbox is not checked it does not includes it in $_POST variable ...
Yes. That's how it is. There is no workaround for this. Using field[] identifiers is only applicable for unstructured input fields. If you depend on the ordering and relation, then unset fields will prevent this from working.
You have no other option but to set explicit indexes. You should bite into the sour apple and do so for name[0], check[1] and select[2]. Use a PHP loop to simplify it:
foreach (range(0,2) as $i)
echo <<< END
<div class="group">
<input type="text" name="name[$i]" /><br />
<input type="checkbox" name="check[$i]" value="true" /><br />
<select name="select[$i]"><option>1</option><option>2</option><option>3</option></select>
</div>
END
I was having the same exact problem with some parts of a form that can be add on user's wish. I came to this really simple workaround using basic javascript:
Add a hidden type input just after your checkbox, assign its value to the state of the checkbox and it's this hidden input that will be reported in your $_POST, will be true or false.
<input type="checkbox" onchange="this.nextSibling.value = this.checked">
<input type="hidden" name="state[]" value="false">
Just change value to true if your checkbox is checked="checked" by default.

determine whether checkbox is checked php $_GET

I just want to have php determines whether a checkbox is checked, but I am running into a problem of getting the right return. Help please.
My html code
<label>
<input name="Language" type="checkbox" id="aa" checked="checked" />
One</label>
<label>
<input name="Language" type="checkbox" id="bb" />Two</label>
<label>
<input type="checkbox" name="Language" id="cc" />Three</label>
I pass the values to php by the $_GET method
my php code:
$aa=$_GET["aa"];
$bb=$_GET["bb"];
$cc=$_GET["cc"];
echo $aa."<br>";
echo $bb."<br>";
echo $cc."<br>";
the output is
true
false
false
I next want to just determine if each box is checked and if so, do something.
if ($aa == true) { $abc="checked";}
else { $abc="not checked"; }
if ($bb == true) { $cde="checked";}
else { $cde="not checked"; }
if ($fgh == true) { $fgh="checked";}
else { $fgh="not checked"; }
But the if statements always return true, even if the box is not checked. I tried variations of "===" and "!=", but it does not seem to work.
TIA
if (isset($_GET['checkbox_name'])) { ... }
Form controls (with the exception of file inputs, and with some special rules for image inputs) always submit strings. There is no concept of a boolean in a query string or a form encoded POST body.
The id is irrelevant — only the name and value matter (at least as far as PHP is concerned).
Since you haven't given them values they will, IIRC, default to on so if you are doing a comparison you should look for that. Looking with isset is simpler though. This is somewhat beside the point though, since your example gives them all the same name and value, so you can't differentiate between them.
Additionally, due to an oddity of the PHP form data parser, you have to end the with [] if you want multiple elements with the same name.
You probably want to do something like this:
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value="One" />
One</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value="Two" />Two</label>
<label>
<input type="checkbox" name="Language[]" id="cc" value="Three" />Three</label>
Important: Note the addition of values and the change in name.
Then in PHP $_GET['Language'] will be an Array of the values of the checked checkboxes, which you can loop over.
Try isset()
I think your HTML code should be like
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value ="1" />One
</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value ="2" />Two
</label>
<label>
<input name="Language[]" type="checkbox" id="cc" value ="3" />Three
</label>
and then by using something like
$count = count($_GET["Language"]); you can count the number of checkboxes checked.
Or do
$arr = $_GET["Language"]; //$arr is an array that contains the values of the checked checkboxes
Then you can foreach over the array
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
In my PHP I got it from $_POST['checkbox_name'], and I found that the variable had the value on when the box was checked (i.e. it existed even if the checkbox was clear).

Categories