I have this simple form:
HTML:
<input type="checkbox" name="product[]" value="20$" /> book
<input type="checkbox" name="product[]" value="30$" /> plane
PHP:
$product = $_POST['product'];
foreach($product as $value) {
echo $value;
}
Note:
The user can choose one or two ... products.
Question:
I want to know if there is a solution to get the name too, like adding an ID class or something ..
Basically I cant get the name attribute because I didn't sent it with the form.
Add the missing information to your POST-parameters:
<input type="checkbox" name="product[book]" value="20$" /> book
<input type="checkbox" name="product[plane]" value="30$" /> plane
You can iterate over it like this:
foreach ($_POST['product'] as $name => $value) {
// ...
}
You can use the option presented by elusive, but it seems that in this case you're passing the price from the client to the server. That might allow people to fraud. They could with only a little hacking send another (lower) price. If you just send 'book' and redetermine the price on the server, this won't be a problem.
If you choose that path, you can just put the product ('book') in the value:
<input type="checkbox" id="book" name="product[]" value="book" />
<label for="book">book</label>
<input type="checkbox" id="plane" name="product[]" value="plane" />
<label for="plane">plane</label>
Related
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]);
I am displaying products from my Product database in a table, with a checkbox to select the product that is desired.
Each product has 3 variants or price point options, entered in the database as seperate items for each price point variation.
Code category Description Points Choose your Points
1001-B Logo artwork supplied Basic 4.00 points
1001-S Logo artwork supplied Standard 6.00 points
1001-P Logo artwork supplied Premium 12.00 points
1002-B Logo re-draw to vector Basic 6.00 points
1002-S Logo re-draw to vector Standard 8.00 points
1002-P Logo re-draw to vector Premium 14.00 points
There are 3 price point variations for every product: Basic, Standard and Premium. Each "Product" has the same Product Code. So as above Product: 1001-B is the Basic, 1001-S is the standard and 1001-P is the premium.
So the customer chooses which Price point option they want by selecting the checkbox next to each item.
I am wanting to only allow them to select one price point / product for each product pairing (ie only one of the 3 price points).
So basically to get the checkbox to work like a radio button. I cannot use a radio button because I am using the same name for all of the items in my checkbox array: name="id[]"
echo '<input type="checkbox" name="id[]" value="' . $id . '" /></td>';
How can I achieve this?
I know I can do the following:
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<p> </p>
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
$("input:checkbox").click(function() {
if ($(this).attr("checked") === true) {
var group = "input:checkbox[name='" + $(this).attr("name") + "']";
$(group).attr("checked", false);
$(this).attr("checked", true);
} else {
$(this).attr("checked", false);
}
});
But this relies on the fact that name="fooby[1][] name="fooby[2][] etc changes. I cannot do that in my case.
Any ideas?
I have already got the cart aspect of this after the page is submitted resolved so I don't want to make fundamental changes to the checkbox format I have.
WHenever you have repeating modules within a page it's easy to isolate instances by traversing up to a main parent of the instance and searching within that parent only.
HTML
<div class="product">
<div class="name"/>
<div class="description/>
<div class="choices">
<input type="checkbox"/>
</div>
</div>
JS:
$('.product input:checkbox').change(function(){
if(this.checked){
$(this).closest('.choices').find('input:checkbox').not(this).prop('checked',false);
}
});
No knowledge of any properties of the checkboxes is required using this generic traverse pattern. If checkbozes aren't wrapped in labels can shorten to:
$(this).siblings().prop('checked',false);
Let's add class attribute to each group and handle with it:
<input type="checkbox" value="1B" name="id[]" class="group1" />
<input type="checkbox" value="1S" name="id[]" class="group1" />
<input type="checkbox" value="1P" name="id[]" class="group1" />
<p> </p>
<input type="checkbox" value="2B" name="id[]" class="group2" />
<input type="checkbox" value="2S" name="id[]" class="group2" />
<input type="checkbox" value="2P" name="id[]" class="group2" />
$("input:checkbox").click(function() {
$('input:checkbox[class="' + $(this).attr('class') + '"]').prop('checked', false);
$(this).prop('checked', true);
});
Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.
I have a multiple checkbox option on my page...
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
</li>
I'm not sure however how to collect all checkbox values using POST method?
if i use
$service = $_POST['service'];
I only get 'other' returned
Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:
Check if a certain value was selected:
if (in_array("Other", $_POST['service'])) { /* Other was selected */}
Get a single newline-separated string with all selected options:
echo implode("\n", $_POST['service']);
Loop through all selected checkboxes:
foreach ($_POST['service'] as $service) {
echo "You selected: $service <br>";
}
Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the
<input type="text" name="other" style="display:none;"/>
and you can show it with javascript when the "Other" box is checked. Something like that.
Just make the name attribute service[]
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br />
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br />
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br />
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br />
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>
Then in your PHP you can access it like so
$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes
etc...
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.
Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"
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).
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.