Item delete buttons in a table? - html

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 } ?>

Related

How to store checkbox values (laravel)

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[]

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.

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.

Why can't I submit my form with WWW::Mechanize::Firefox in Perl?

I have an HTML this is part of the form I'm working on
<form action="/goform/FormUpdateVAP" autocomplete="off" name="myform" id="formid" method="POST">
<table>
<tbody>
<tr>
<td align="right"><span class="label">Name:</span></td>
<td><input id="essid" name="essid" value="X" type="text"></td>
</tr>
<tr>
<td align="right"><input id="broadcast_essid" name="broadcast_essid" value="any" checked="" type="checkbox"></td>
<td><span class="label">Broadcast name:</span></td>
</tr>
</tbody>
</table>
<!-- BEGIN RIGHT COLUMN -->
<table>
<tbody><tr>
<td>
<input name="authentication" id="authentication" value="any" onclick="Update();" type="checkbox"><span class="label"><b>authentication</b></span>
</td>
<td>
<input onclick="Update();" name="wp" id="wp" value="any" type="checkbox"><span class="label"><b>Wp</b></span>
<select id="8021x_mode" name="8021x_mode" onchange="Update();"><option value="wpa">WPA</option>
<option value="wep">WEP</option>
</select>
</td>
</tr>
<tr>
<td height="26">
<input onclick="Update();" name="w_p" id="w_p" value="any" type="checkbox"><span class="label"><b>Wp</b></span>
<select id="8021x_mode" name="8021x_mode" onchange="Update();"><option value="wpa">WPA</option>
<option value="wep">WEP</option>
</select>
</td>
</tr>
<tr>
<td>
<input id="cancel" name="cancel" value="Cancel" onclick="window.location.href = '/fg/list.asp';" type="button">
</td>
<td>
<input id="add-2" name="add" value="Save" type="submit">
</td>
</tr>
</tbody>
</table>
I'm trying to submit it filling the Name and the Broadcast name. And checking the 8021x_mode and the wp checkboxes. If I tick 8021x_mode and set the fields the form submits just fine. But when I try ticking wp it doesn't submit. I got no error messages.
Here's my code so far:
use WWW::Mechanize::Firefox;
use Crypt::SSLeay;
use URI::Fetch;
use HTML::TagParser;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; #not verifying certificate
my $url = 'https://';
$url = $url.#ARGV[0];
my $mech = WWW::Mechanize::Firefox->new;
$mech->get($url);
$mech->form_id('formid');
$mech->tick( '8021x_mode','any');
$mech->tick( 'wp','any'); --after I add this the form doesn't submit
my $name = #ARGV[1];
$mech->set_fields(
'vap_name' => $name,
'essid' => $name,
);
$mech->click_button( id => 'add-2' );
$mech->reload();
add-2 is the submit button. Any idea why is not working? If you need more information please let me now. Any help will be highly appreciated.
The problem is that the html doesn't contains the choice any for 8021x_mode.
I get this error :
No elements found for Checkbox with name '8021x_mode' and value 'any'
at test.pl line 24.
To check the form, I recommend you the mech-dump utility (installed with WWW::Mechanize)
$ mech-dump --forms http://domain.tld/path_to_forms
POST /goform/FormUpdateVAP [myform]
essid=X (text)
broadcast_essid=<UNDEF> (checkbox) [*<UNDEF>/off|any]
authentication=<UNDEF> (checkbox) [*<UNDEF>/off|any/authentication]
wp=<UNDEF> (checkbox) [*<UNDEF>/off|any/Wp]
8021x_mode=wpa (option) [*wpa/WPA|wep/WEP]
w_p=<UNDEF> (checkbox) [*<UNDEF>/off|any/Wp]
8021x_mode=wpa (option) [*wpa/WPA|wep/WEP]
cancel=Cancel (button)
add=Save (submit)
As you can see, there's no any choice...
Maybe the javascript there is doing some things that we don't know.
A workaround is to use LiveHttpHeaders Firefox addon to catch the forms POSTed to /goform/FormUpdateVAP and then instead of ticking, do a POST request with WWW::Mechanize.

how to pass value of checkboxes using ahref

I am using following code in my project
<cfoutput query="getOptions">
<tr>
<td align="center"> #optionname#</td>
<td align="center"> #DollarFormat(optionprice)#</td>
<td><input type="checkbox" name="OptionalID" value="#OptionID#" ></td>
</tr>
</cfoutput>
And i am passing the value to other form as follows
<a href="addtocart.cfm?pid=#productId#&OptionalID=#OptionalID#">
what should i do to pass all values of all the checkboxes that are checked.Please help
Thanks in advance
Change your page to use a form instead of passing the values via a link.
Something along the lines of:
<form name="myform" action="addToCart.cfm" method="post">
<cfoutput>
<input type="hidden name="pid" value="#productId#">
<input type="hidden name="OptionalID" value="#OptionalID#">
<cfloop query="getOptions">
<tr>
<td align="center"> #optionname#</td>
<td align="center"> #DollarFormat(optionprice)#</td>
<td><input type="checkbox" name="OptionalID" value="#OptionID#"></td>
</tr>
</cfloop>
<input type="submit" value="Add to cart">
</cfoutput>
</form>
You can pass as many values as you want now, and the checkboxes will come up as a list.
hope that helps
Though you can get tricky with JavaScript, if you use an A to add to cart, you'll only be passing the values in the URL. You want to use a form instead.
<form name="cartForm" action="addtocart.cfm?pid=#productId#&OptionalID=#OptionalID#" method="POST">
. . . .
</form>
Of course, this means you'd ordinarily use a submit button inside your form. If you're set on using a link, you can do
Add to cart
(note that I've omitted various tags and escaping of # for simplicity)
You need JavaScript for that.
First, have this in your page where you have the code you posted:
<script type="text/javascript">
function AddCheckboxValues(oLink, sName) {
var arrCheckboxes = document.getElementsByName(sName);
var values = [];
for (var i = 0; i < arrCheckboxes.length; i++) {
if (arrCheckboxes[i].checked) {
values.push(arrCheckboxes[i].value);
}
}
oLink.href += "&" + sName + "=" + values.join(",");
}
</script>
Second, add onclick to the link tag:
<a href="addtocart.cfm?pid=#productId#" onclick="AddCheckboxValues(this, 'OptionalID');">
That's it, now the form addtocart.cfm will get query string values of the checkboxes ticked by the user.