How to store checkbox values (laravel) - laravel-5.4

I have List of the infraction and All infraction is print in a foreach loop. I want to store some infraction id in the database using checkbox.
<table class="table table-bordered">
#foreach($infractions as $infraction)
<tr>
<th>{{ $infraction->title }}</th>
<td>
<input type="checkbox" value="{{ $infraction->id }}" name="infraction_id[{{ $infraction->id }}]" data-toggle="toggle" data-on="Yes" data-off="No" data-onstyle="success" data-offstyle="danger">
</td>
</tr>
#endforeach
</table>
I have no idea where I put submit button and how to get value from a checkbox into Controller to save data.
public function store(Request $request)
{
if($request['infraction_id']!= null){
foreach ($request['infraction_id'] as $inf){
$query="insert into inspection ('title') value ($infraction->id)";
}
}
auth()->user()->inspections()->create($request->all());
}
I want to change php (a example) to laravel.

Change chceckbox name to
name=infraction_id[]

Related

Get the selected value in a select - option Thymeleaf and Spring Boot

Im trying to generate a search options and in myweb page the user choose the value that is looking for this is my html code:
<table>
<tr>
<td><h2 align="left">Red Social: </h2></td>
<td>
<select th:field="*{redes}" name="redSoc">
<option th:each="r : ${redes}" th:value="${r.nombreRedSocial}"
th:text="${r.nombreRedSocial}"></option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Buscar"/></td>
</tr>
</table>
And this is my controller code:
#GetMapping("/RedesSociales")
public String infoPorRedSocial(Model model, #RequestParam(name = "redSoc", required = false) String redSoc) {
RedesSociales[] redes = redesService.obtenerTodasRedes();
model.addAttribute("redes", redes);
//model.addAttribute("grupo", null);
System.out.println(redSoc);
if (redSoc != null) {
System.out.println(redSoc);
model.addAttribute("info", infoService.obtenerTodosPorRedSocial(redSoc));
}
return "visual/reportes/ListarPorRedSocial";
}
Now as you can see im trying to get the value redSoc but im not sure how to the get this value from the selected option i tried several methods but didnt work.
EDIT:
I was using this code:
<form action="/RedesSociales" method="GET">
<h2 align="left">Red Social: </h2>
<input type="text" name="redSoc"/> <br>
<input type="submit" value="Buscar"/>
</form>
But this was not usefull because the option would be insert by the user and i wanted to made it more dynamic thats why now im using the current values stored in the database
already solve the problem Change my code to this :
<table> <tr> <td><h2 align="left">Red Social: </h2></td>
<td> <select name="redSoc"> <option th:each="r : ${redes}" th:value="${r.nombreRedSocial}" th:text="${r.nombreRedSocial}">
</option>
</select> </td> </tr>
<tr> <td><input type="submit" value="Buscar"/></td> </tr> </table>

how to bind dynamic generated inputs to $scope

I am retrieving items from db put them in dir-paginate or ng-repeat what i am trying here to pass id in as model name to get key and name also as key as i am fetching a array to create many inputs and retrieve them in controller, but when i retrieve ng-model object values with console.log($scope.values); it says undefined.
how i will pass multiple generated inputs to controller in scope object is my question even ng-click dont work ??
view
<form angular-validator-submit="submitForm()"
name="submit-form" class="form-horizontal" novalidate
angular-validator>
<table>
<thead>
<tr>
<th>S.No</th>
<th>id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr dir-paginate="x in items| itemsPerPage: pageSize" current-page="currentPage">
<td><input type="text" name="id" ng-model="values.{{x.id}}[$index]"></td>
<td><input type="text" name="test" ng-model="values.{{x.name}}[$index]"></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary">Save</button>
</form>
$scope.submitform = function(){
console.log($scope.values);
}
ng-model="values[x.id]" will do the work and define $scope.values = []; as suggested in comments.

Need to bind data

Following is my code, I need to bind data to ng-model, when I bind ng-model = "value", I can see the value in the text field, but I want to update the value on changing the text input.
<tbody ng-repeat="action in model.editAction">
<tr ng-repeat="(key, value) in action | orderBy: '-key'">
<th> {{ key }} </th>
<td>
<input type="text" class="form-control"
ng-model="model.editAction.value" name = "key"
required/>
<span style="color:red" ng-show="main.AssetURL.$error.required ">Service name</span>
</td>
<td> {{ action.DESCRIPTION }} </td>
</tr>
</tbody>
In your controller define a $scope var like this:
$scope.value
In your input, place just value inside ng-model:
ng-model="value"
This way you are binding values between your view and your controller.
You can use ng-change for getting the new values which you input
<input type="text" class="form-control"
ng-model="model.editAction.value" name = "key" ng-change="onChangeValue(model.editAction.value)"
required/>
And in your controller,
$scope.onChangeValue=function(value){
console.log(value);
//you should see the new updated value
}

using a table name by php in a submit form

I want the user choose a table name in mysql database and then I can add a data by textbox of a form inside the field.
I used PHP $_POST[ ] to send table name to the adding form. but in the adding form; when I submit the add button; the table name has been lost.
I have searched a lot and tested session and constant to hold the table name value; but
didn't get the desired result.
Any help will be greatly appreciated.
here is the code of the page which selecting the table name:
<form action="change_products.php" method="post" name="select_table">
<select name="edit_products" >
<option <?php if($_POST['edit_products'] == "cabin") echo "selected='selected'"; ?> value="cabin">Cabin case</option>
<option <?php if($_POST['edit_products'] == "cabin_door") echo "selected='selected'"; ?> value="cabin_door">Cabin door</option>
</select>
<input type="submit" value="OK">
</form>
and this is the code of the adding form in "change_products.php" page.
<?
include('functions.php'); //the database info
define('selected_table', $_POST['edit_products']);
?>
<form method="post">
<table>
<tr>
<td>Mark:</td>
<td><input type="text" name="mark" /></td>
</tr>
<tr>
<td><input type="submit" name="add" value="New" /></td>
</tr>
</table>
<?
if (isset($_POST['add']))
{
$mark=$_POST['mark'];
mysql_query("INSERT INTO ".selected_table." (mark) VALUES ('$mark')");
}
?>
</form>
You didn't set $_POST['add'], thus, the last part of PHP code in change_products.php does not run.

Item delete buttons in a table?

this is a simple html question but I would like to know the correct way of doing it.
I have a table in my page, with tabular data, and it's a display of items in the database. I build the table dynamically with php, and just output html.
I want to add a column "delete this entry" with a button/link/picture on every line, and when it's clicked, my page refreshes and php knows which itemID should be deleted.
The question is not about php, but about the html part.
How can I make the delete-button compliant with standards? Here's an incorrect solution:
<table>
<form action='#' method='post'>
<tr>
<td>Item 1</td>
<td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
</tr>
</form>
<form action='#' method='post'>
<tr>
<td>Item 2</td>
<td><input type='hidden' name='id' value='2'><input type='submit' value='X'></td>
</tr>
</form>
</table>
Anyone an idea on a good solution? I'd prefer not to use much javascript, if it can be avoided.
Thanks in advance!
Edit: actually, I have a secret second question too: what if i were to want to do inline editing (after pressing an Edit button), as in: make the cells with the values contain boxes, and a submit button at the last cell?
Why not isolate the forms and pass the ID to the action page?
<table>
<tr>
<td>Item 1</td>
<td><form action='action.php?id=1' method='post'><input type='submit' value='X'></form></td>
</tr>
<tr>
<td>Item 2</td>
<td><form action='action.php?id=2' method='post'><input type='submit' value='X'></form></td>
</tr>
</table>
You can write names of submit buttons as set of facts and their values, for example name='set1(fact1:value2,fact2:value2,...)'. Then search for submitted set and get fact value. A PHP class PageFacts below can do this. You can check if set is posted using condition if (PageFacts::is('del')) and get value of fact with statement $itemID = PageFacts::get('del', 'id').
Form with submit buttons can look like:
<form method="post">
... item [0] text/input
<input type="submit" name="del(id:0)" value="Del">
... item [1] text/input
<input type="submit" name="del(id:1)" value="Del">
... item [2] text/input
<input type="submit" name="del(id:2)" value="Del">
...
...
<input type="submit" name="chng" value="Chng">
</form>
An example of PHP code that deletes table item with itemID can be:
if (PageFacts::is('del')) {
$itemID = PageFacts::get('del', 'id');
// Delete $itemID
deleteTableItem($itemID);
}
// or for multiple facts like del(cid:2,id:0)
if (PageFacts::is('del')) {
$cartID = PageFacts::get('del', 'cid');
$itemID = PageFacts::get('del', 'id');
// Delete $itemID from $cartID
deleteCartItem($cartID, $itemID);
}
PageFacts PHP code:
class PageFacts {
// Gets fact value from posted set
// $s ... name of a set
// $f ... name of a fact
// returns - value of fact $f in set $s
// - TRUE if set $s exists but fact $f doesn't exist
// - FALSE if set $s doesn't exist
public static function get($s, $f = NULL) {
// Regex pattern to search for set $s
$ps = "/^\s*$s\s*\((.*)\)\s*$/";
// Search POSTed names (variable $v is not used)
foreach ($_POST as $key => $v) {
$ok = preg_match($ps, $key, $matches);
if ($ok && isset($matches[1])) {
// If $f is not passed return TRUE, means set $s exists regardless of fact $f
if (!isset($f)) {
return TRUE;
}
// Otherwise return value of fact $f
// use regex pattern to search in list of fact:value items separated by a comma
$pf = "/^\s*$f\s*\:\s*(.+)\s*$/";
foreach (explode(',', $matches[1]) as $fv) {
$ok = preg_match($pf, $fv, $matches);
if ($ok && isset($matches[1])) {
return $matches[1];
}
}
}
}
return FALSE;
}
// Checks if set $s is posted
public static function is($s) {
return self::get($s);
}
}
It's kind of messy, but going off of what Agent said, you could do something like this to get a text input in there too:
<form action='action.php?id=1' method='POST'>
<table>
<tr>
<td>Item 1</td>
<td><input type="text" value="" /></td>
<td><input type='submit' value='X'></td>
</tr>
</table>
</form>
<form action='action.php?id=2' method='POST'>
<table>
<tr>
<td>Item 2</td>
<td><input type="text" value="" /></td>
<td><input type='submit' value='X'></td>
</tr>
</table>
</form>
Add a submit button to each form and send the operation you want to execute as value attribute of each button
<table>
<form action='#' method='post'>
<tr>
<td>Item 1</td>
<td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
<td><button type="submit" value="remove" name="op">Remove</button></td>
<td><button type="submit" value="edit" name="op">Edit</button></td>
</tr>
</form>
</table>
You could go with a simple authenticated GET procedure, like this:
Delete
Note the action and item values in the URL. These would be evaluated before the database is called and the results are printed in showResults.php. Your PHP (on showResults.php) would start with a block of logic that checks to see if any actions were requested, such as delete. If an action is present, it checks to see if an item is as well. I will then do whatever authentication necessary to ensure the user has sufficient rights to do this action to this item.
Next it would query the database (now lacking said item assuming the deletion was successful) and redraw the page. The links would be built within your loop used to create the many table-rows:
<?php foreach ($products as $product) { ?>
<tr>
<td>
Delete
</td>
<td>
<p><?php print $product->title; ?></p>
</td>
</tr>
<?php } ?>