my first post here, and first proper Laravel project.
I have a table, from which I am pulling out at random 3 values. Each of those contain an (1) action, that I am printing, and a (2) number of points associated with this action. The table currently does not have any more rows.
Each of the actions when displaying is now assigned a checkbox, which user can tick (or not) before submitting the form. Currently it looks like this:
#foreach (App\Models\Action::randomiser() AS $action)
<label class="container"><sup>{{ $action->action }}</sup>
<input type="checkbox" name="action[]" value="{{ $action->point }}">
<span class="checkmark"></span>
</label>
#endforeach
The trouble is, I need to collect the values of each ticked box. Now, the method is POST, but the there is no table of reference for it - you tick the boxes, and submit, and output comes back, or at least it COULD come back if I could access the value of the action.
When I've done it in similar fashion, but without using foreach, and I could access value quite easily, and all of it added nicely together:
$total = (
+(isset($_POST["action[1]"]) ? ($_POST["action[1]"]): 0) +
+(isset($_POST["action[2]"]) ? ($_POST["action[2]"]): 0) +
+(isset($_POST["action[0]"]) ? ($_POST["action[0]"]): 0));
then $total gets evaluated in next function.
I tried to access it via each item in an array, using +(isset($_POST["action[0]"]) etc, but that comes back with 0 no matter how many items I tick in .
Is there some smart way to access these values? Or if that's impossible, can you perhaps suggest best way to create the table to collect the information? I will need to pull foreign key of $action->point, and each value would have to count as boolean (true or not true)...
Any help would be much appreciated.
Related
I'm creating a CMS in which I have an overview of pages. I want the user to be able to mass delete these pages and so I have created a form in which each page has a checkbox with the pages database ID as value and name:
<input class="mass-delete-check" type="checkbox" name="<?=$page["id"]?>" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
Now when I submit this form I need to get the values of the checkboxes that are actually checked and put them in an array I can go through to delete them. The thing here is that I will have to get checkbox values based on if they are checked and not on their name because I can't know all names.
Does anyone have a solution to this?
Use the same name for all checkboxes. So after submiting you will have array with page IDs to delete.
<input class="mass-delete-check" type="checkbox" name="delete_pages[]" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
After submit you would get array of IDs with $_POST['delete_pages'], which contains actual page IDs what you need to delete.
I have a table of items, and if the user selects an item I need to know how many units are assigned to this item. So I created a check box list coming back with the selected items ids and another one for units.
<td>{{$item->name}}</td>
<td>{{Form::checkbox('itemchks[]', $item->id)}}</td>
<td>{{Form::text('units[]','0')}}</td>
The problem is that, unless the user checks all items we get 2 different-size lists.
For ex, when choosing 3 items, and assigning them some units values, the itemchks come back with these ids
["4","15","23"]
but the units list returns
["15","0","18","0","0","0","0","0","20"]
so I can't know, exactly, each id and its correspondent units.
I'm using laravel 4.2
Any ideas will be appreciated.
Instead of letting the browser set the key in your units[] array you could do that yourself using the $item->id:
<td>{{$item->name}}</td>
<td>{{Form::checkbox('itemchks[]', $item->id)}}</td>
<td>{{Form::text('units['.$item->id.']','0')}}</td>
I want to show values in a dropdown box (either from an array or database - please advise on which option is method)
Then
when a user selects a value in dropdown box, i want to have and get its associated index value (like 1, 2).
For example: dropdown box shows values:
"Car"
"Bicycle"
If user selects "Car", when i get dropdown selected value, i should get 1, similarly for "Bicycle" i get 2 .. and so on.
Please advise easy and simplest method to implement this
Thanks
You can use two methods:
1) Set up prior to display a code that holds associative array with key -> value i.e: 1 => Car (you can keep it in config file if it doesn't change frequently, you can pull it from database or you can keep it in some other form: serialized, file etc.) and use it when the submitted form is being processed.
2) Use array with key and value with the same string i.e: Car => Car and when the form is processed you will have value right away. This solution has some limitations and be troublesome with using more words or other characters that need to be sanitized.
I would advise option 1, you will have to set up the list before and use it after form has been processed but it allows more freedom and its less maintenance.
I want to create a PHP script which can fill up a form from a text file. The form is on a password-protected page. There are 4 fields where i need to put text (name, village, etc.)
I need to select checkboxes but they are in an array and I only know their label name. At max I can look up id's and use it as static values, but I don't know them how to use them either.
<form method="POST" action="...">
<input type="checkbox" name="tag_id[]" id="tag_id_192" value="192"><label for="tag_id_192">House</label>
I saw lots of tutorials which shows how to do it with checkboxes but they used the check box name and without the login part. I never used PHP before so I don't know even where to start. If someone could guide i would be very grateful.
Example:
The .txt file each row represents a form fill up the fields are separated with a *: Name * village .... * the check boxes label names which i need to set active
If you're using cURL, you can post multiple fields with the same name, if it has [] after the name, php will automatically concatenate them all into an array..
So its valid to do tag_id[]=192&tag_id[]=175&tag_id=[285]..
In PHP it will know to make it array(192,175,285)
As a side note, fields that do not have [] get overwritten each time they appear...
I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.
<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);
it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button
You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}