I can't get the value of the check box field. This is the structure of my code
<form action="<?php echo base_url()?>Home/dashboard" method="post">
<button class="btn btn-default" type="submit" name="delete" onclick="cliked1(event)"
style="margin-left:2px;background-color:blue;color:white;">Delete Item</button>
<td><input class="checkbox" type="checkbox"name="cb" value="1"> 1 </td>
</form>
In the controller
else if(isset($_POST['delete'])) {
$cb = $this->input->post('cb');
echo $cb;
}
I recreated the process it in other file and it works
<form action ="<?php echo base_url()?>Shop/test" method="POST">
<button class='btn btn-danger btn-xs' type="submit" name="delete" value="delete"><span class="fa fa-times"></span> delete</button>
<input type="checkbox" name="cb[]" value="lol">
<br>
<input type="checkbox" name="cb[]" value="yeaa">
</form>
In the Controller
function test()
{
$this->load->view('test');
if(isset($_POST['delete']))
{
$id = implode('^',$this->input->post('cb'));
print_r($id);
}
}
And I apply this process and it turns like this
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<th>ID</th>
<th>Item Photo</th>
<th>Item Name</th>
<th>Stocks</th>
<th>Date Posted</th>
<th>Price</th>
<th>Actions</th>
</thead>
<tbody>
<form action ="<?php echo base_url()?>Shop/test" method="POST">
<?php
$x=1;
foreach($shop_items as $key)
{
?>
<tr>
<td><input class="checkbox" type="checkbox" name="cb[]" value="<?php $key->shop_item_id?>"> <?php echo $x;?> </td>
<td><center><img src="<?php echo base_url()?>uploads/items_main/<?php echo $key->shop_item_main_pic;?>" style = "width:60px;height:50px;"alt=""></center></td>
<td><?php echo mb_strimwidth($key->shop_item_name, 0, 18,"...");?></td>
<td style="width:10px;"><center><button><span class="fa fa-eye"></span></button></center></td>
<td><?php echo date('F,d,Y',strtotime($key->shop_item_date_posted))?></td>
<td><?php if(!$key->shop_item_sale){ echo number_format($key->shop_item_orig_price);}
else{ echo "<font color='red'>".number_format(intval($key->shop_item_orig_price-($key->shop_item_orig_price*($key->shop_item_sale/100))))."</font> ";}?></td>
<td>
Bid | View
</td>
</tr>
<?php
$x+=1;
}
?>
<button class='btn btn-danger btn-xs' type="submit" name="delete" value="delete"><span class="fa fa-times"></span> delete</button>
</form>
</tbody>
</table>
The problem is that the submit button does not work.
In case of a checkbox, when the checkbox is not checked, you will not get the checkbox set in the $_POST. So to get the value of checkbox, you need to do this:
if (isset($_POST["cb"])) {
// Checkbox is checked.
$cb = $_POST["cb"];
} else {
// Checkbox is not checked.
}
And please do not check on type="submit".
From one of the resources:
In most cases this will work, but there are a few occasions when it won't. To identify when this condition will fail we have to look at the different ways of submitting a form.
There are two ways of submitting a form, one is to click the submit button, the other is to hit return, of which will invoke the submit button. The problem occurs when you hit the return button (as most people do), when using Internet Explorer (the most commonly used browser) and when the submit button does not have focus. This will submit the form, but it will not send the submit variable, which would mean the script we are using above will fail.
Related
How do I fix this issue, you see in the picture below, my code creates a table successfully and echos the first item correctly, but if any other item is added to the cart, it gets displayed below the table, outside the border, not in a row.
Desired Result: For 'Sara Lee Muffin' and the quantity, and price (example) to be inside the table in it's own row and remove button. (Basically set out the exact same way a the Toasted Sandwhich, but below it)
My code is below the image.
<body>
<br>
<h3>Current shopping cart:</h3>
<table style="border: 1px solid black;">
<?php
//If the cart is not empty, then create a table for the Items
if(!empty($_SESSION['shopping_cart'])) {
?>
<tr><th>Item name</th><th>Quantity</th><th>Price</th><th></th></tr>
<?php
if(!empty($_SESSION['shopping_cart'])) {
foreach($_SESSION['shopping_cart'] as $keys => $values) {
?>
<tr>
<td style="text-align:center"><?php echo $values['item_name'];?></td>
<td style="text-align:center"><?php echo $values['item_quantity'];?></td>
<td style="text-align:center">$<?php echo $values['item_price']*$values['item_quantity'];?>
</td>
<td><form action="" method="get">
<input type="submit" class="btn btn-danger" name="remove" value="Remove">
<input type="hidden" name="hidden_id" value="<?php echo $values['item_id']; ?>">
</form></td>
</tr>
</table>
<br>
<?php
}
}
?>
<form action="" method="get">
<input type="submit" class="btn btn-primary" name="payment" value="Confirm">
</form>
<?php
}
else {
echo "No Items added yet.";
?><br>
<?php
}
?>
</body>
You have a closing </table> inside your foreach loop, so every item in the array prints out a </table> tag.
You should add another check for the shopping cart after the loop, and if it exists, print the closing </table> tag.
i am trying to delete one row from a table based on the selection of the user:
<form method="post" action="updatecart.php">
<td> <button class="btn" type="submit" name="update" >Update</button></td>
<td> <button class="btn" type="submit" name="remove" >Remove</button></td>
<input type="hidden" name="itemid[]" value=<?php echo"$row[3]"?>>
<input type="hidden" name="cartid[]" value=<?php echo"$row[6]"?>>
<td><img alt="" src="/photos/<?php echo"$row[4]"?> " width= 100 height=100></td>
<td><?php echo"$row[0]"?></td>
<td><input type="number" id="quantity" name="quantity[]" min=1 max=<?php echo"$row[5]"?> value=<?php
echo"$row[1]"?>></td>
<td><?php echo"$row[2]"?></td>
<?php $total = $total * $row[1]?>
<td><?php echo"$total"?></td>
</tr>
<?php $totaloftotal+=$total; ?>
whenever the use presses the remove button only the selected item, but what happens is everything gets deleted.
this is the
elseif(isset($remove)){
for($z= 0; $z <count($itemid); $z++ ){
try{
require('connection.php');
$cartid= $_POST["cartid"];
$itemid= $_POST["itemid"];
$qty= $_POST["quantity"];
$sql2= "delete from cart where qty=$qty[$z] and iid=$itemid[$z] and cart_id=$cartid[$z] and uid=$uid";
$x = $db->exec($sql2);
the update works just fine, i only have a problem with the delete.. help.
Perhaps the following might give you an idea of how you might proceed. The PHP that processes the POST request has been designed to work with Prepared Statements but is not yet complete and I will stress that this is not tested!
Please look through the html and read the comments which hopefully explain what is going on at that point. Obviously the content of the table ( ie: each table row and cells etc ) will be dynamically generated by db query results - below is static for demo.
The pseudo db processing code here uses mysqli but it looks like you are using PDO so use that as a guide only... look at Prepared Statements here
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
</head>
<body>
<!--
using a single form to process multiple records requires
a little more consideration than for a single record.
Submitting the form "as-is" leaves you with no way to know
which particular record PHP should update because they are
all suitable candidates potentially.
One method I made reference to was to use a single form with
just the fields required for processing that are populated
by javascript. To that end...
* Assign a name to the form for easy identification.
* remove the `[]` from field names in the form.
* remove the two hidden fields and assign `dataset` attributes to each button
The table below is a loose facsimile of what I deduced the
original table to look like...more or less.
-->
<form method='post' name='update' action='updatecart.php'>
<table>
<tr>
<th>Update</th>
<th>Remove</th>
<th>Image & Link</th>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>
<tr>
<td><button class="btn" type="button" name="update" data-cartid="23" data-itemid="1">Update</button></td>
<td><button class="btn" type="button" name="remove" data-cartid="23" data-itemid="1">Remove</button></td>
<td><img alt="" src="/photos/banana.jpg" width=100 height=100 /></td>
<td>Senga-X</td>
<td><input type="number" name="quantity" min=1 max=74 value=17 /></td>
<td>43</td>
<td>93</td>
</tr>
<tr>
<td><button class="btn" type="button" name="update" data-cartid="69" data-itemid="1">Update</button></td>
<td><button class="btn" type="button" name="remove" data-cartid="69" data-itemid="1">Remove</button></td>
<td><img alt="" src="/photos/apple.jpg" width=100 height=100 /></td>
<td>Senga-Y</td>
<td><input type="number" name="quantity" min=1 max=45 value=10 /></td>
<td>35</td>
<td>1505</td>
</tr>
</table>
<!--
use only the number of hidden elements as are
needed in the sql later for update/delete
-->
<input type='hidden' name='cartid' />
<input type='hidden' name='itemid' />
<input type='hidden' name='qty' />
<input type='hidden' name='task' />
</form>
<script>
document.querySelectorAll('button[name="update"],button[name="remove"]').forEach( bttn=>{
bttn.addEventListener('click',function(e){
// stop the form from being submitted conventionally
e.preventDefault();
// obtain reference to the form
let form=document.forms.update;
// set values of hidden elements based upon the particular button that was clicked
form.cartid.value=this.dataset.cartid;
form.itemid.value=this.dataset.itemid;
form.qty.value=this.parentNode.parentNode.querySelector('input[name="quantity"]').value;
form.task.value=this.name;
// send your form with only these values!
form.submit();
});
});
</script>
</body>
</html>
And the PHP to process the request:
<?php
#updatecart.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xxx';
$dbname = 'xxx';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['itemid'], $_POST['cartid'], $_POST['qty'], $_POST['task'] ) ){
$cartid=$_POST['cartid'];
$itemid=$_POST['itemid'];
$qty=$_POST['qty'];
$task=$_POST['task'];
switch($task){
case 'remove':
$sql='delete from cart where cart_id=?';
$stmt=$db->prepare( $sql );
$stmt->bind_param('s',$cartid);
break;
case 'update':
$sql='update cart set qty=? where cart_id=?';
$stmt=$db->prepare($sql);
$stmr->bind_param('ss',$qty,$cartid);
break;
}
#etc etc etc
exit();
}
?>
I´m passing a value of a radio button in a page with a form and I want to get the value of the selected option in another php file, but I can´t get that value.
First page:
<form class="itemSelection" action="../php/itemSelection1Save.php"><br><br>
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" id="selectedItem" required="" value="<?php echo $row[0] ?>"></td>
<td><?php
echo row[0]</td>
</tr>
<?php
}
?>
</tbody>
</table><br>
<input type="submit" value="Next"/>
</form>
Second file:
$_SESSION['selectedItem1'] = filter_input(INPUT_POST, 'selectedItem');
filter_input(INPUT_POST, 'selectedItem')
returns nothing
You form is with method get (the default method if not specified):
<form class="itemSelection" action="../php/itemSelection1Save.php">
insert method='post':
<form class="itemSelection" action="../php/itemSelection1Save.php" method='post'>
Make the following changes in HTML part , add method="post"
<form class="itemSelection" action="../php/itemSelection1Save.php" method="post">
Make the following change in PHP part (optional):
Also add filter_option in filter_input. filter the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering.
filter_input(INPUT_POST, 'selectedItem',filter_option)
You Need To Add The Form Method you can try my code i simply code for you hopefully it will work!
<form class="itemSelection" method="post" action="../php/itemSelection1Save.php">
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" value="<?php echo $row[0]; ?>"></td>
<td><?php echo $row[0]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" name="submit" value="Next">
</form>
I'm pulling an array of objects from my database and iterating through them using PHP to display a table. Each result row of the table has the option of deleting that particular record from the database. Each row is also an object, and the object type has a method that will delete itself from the database.
The problem I'm having is thinking through how to get this to work in the view. Right now if you click the delete button, it sets a POST variable to "delete" and reloads the same page wherein a listener process detects the variable and initiates the delete via a hidden input that contains the records database id.
This is all well and good, but these are all objects. Shouldn't each object be able to access it's own methods? It seems the only barrier to this is the interplay between PHP script and HTML forms. My code is below. Ideally, when the delete button is pressed, that particular instance of the object teamleader can run its method teamleader->delete_team_leader().
<?php foreach($teamleaders as $teamleader) { ?>
<tr>
<td><?php echo $teamleader->first_name; ?></td>
<td><?php echo $teamleader->last_name; ?></td>
<td><img src="<?php echo '../img/' . $teamleader->photo;?>" alt="" class="img-fluid tiny-tl-img"></td>
<td><?php echo nl2br(substr($teamleader->bio, 0, 100));?>...</td>
<td><?php echo $teamleader->url_name;?></td>
<form action="index.php?a=edittl" method="post">
<td>
<input type="hidden" name="id" value="<?php echo $teamleader->id?>">
<input type="submit" class="btn btn-default" name="edit" value="Edit">
</td>
</form>
<form action="index.php" method="post">
<td>
<input type="hidden" value="<?php echo $teamleader->id?>" name="id">
<input type="submit" class="btn btn-danger" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete this team leader?')">
</td>
</form>
</tr>
<?php } ?>
In your case, the best solution I think you can use ajax/jQuery for editing or deleting action.
You don't need to use many forms as now.
The code in front-end like this:
<head>
<script src="jquery.min.js"></script>
</head>
...
<?php foreach($teamleaders as $teamleader) { ?>
...
<td><?php echo $teamleader->url_name;?></td>
<td>
<input type="submit" class="btn btn-default btn-edit" name="edit" value="Edit" data-teamleaderid="<?php echo $teamleader->id?>">
</td>
<td>
<input type="submit" class="btn btn-danger btn-delete" name="delete" value="Delete" data-teamleaderid="<?php echo $teamleader->id?>">
</td>
</tr>
<?php } ?>
And the code with using ajax/jQuery is:
$(document).ready(function(){
$('.btn-delete').on('click',function(){
var _this = $(this);
var _teamleaderid = $(this).data('teamleaderid');
if (!confirm('Are you sure you want to delete this team leader?')) return false;
$.ajax(
url:'delete.php',
type:'post',
data: {
teamleaderid: _teamleaderid
},
dataType:'json',
success:function(data) {
if (data.return) {
$(_this).parent().parent().remove();/*Remove the current row in table html*/
alert('This teamleader has been deleted');
}
}
);
});
});
The delete.php code in backend is:
<?php
... #connect database
$teamleader_id = filter_input(INPUT_POST,'teamleader');
$check_delete = $dbconn->query("DELETE from teamleader_table WHERE id={$teamleader_id}");
print json_encode('return'=>$check_delete);
die();
?>
I want make checkbox for checking all sub checkboxes.
My checkbox:
<table width="30%" class="table striped hovered cell-hovered border bordered">
<tr valign="middle">
<td><b>IDPEL</b></td>
<td><b>No. Baris</b></td>
<td><b><input type="checkbox" id="pilihsemua"/> Pilih Semua</b></td>
</tr>
<?php
foreach ($panel_error as $key) {
echo"<tr><td>".$key->errpanel."</td>";
echo"<td>".$key->nomorBaris."</td>";
echo"<td>";
echo form_checkbox('chk_boxes1[]',$key->errpanel);
echo"</td></tr>";
}
?>
</table>
and here is my script:
<script type="text/javascript">
$(document).ready(function () {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',checked)
})
$('#table1').dataTable();
$('#table2').dataTable();
//checkbox
$("#pilihsemua").click(function () { // If #pilihsemua checked, all checkbox will be checked.
$('.chk_boxes1[]').attr('checked', checked);
});
// if all sub checkboxes are being checked, #pilihsemua will automatically checked.
$(".chk_boxes1[]").click(function(){
if($(".chk_boxes1[]").length == $(".chk_boxes1[]:checked").length) {
$("#pilihsemua").attr("checked", "checked");
} else {
$("#pilihsemua").removeAttr("checked");
}
});
//end of checkbox
});
</script>
But still, I don't know why, it can't be work. I try to check #pilihsemua but all the sub classes doesn't be checked. Or if I checked all the sub classes, the #pilihsemuadoesn't be checked too.
Maybe Its close to this:
But this script has some flaws.. Just try to figure out whats wrong..
Example:
HTML CODE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<table width="30%" cellspacing="1" cellpadding="1" border="collapse">
<tr>
<td><b><input type="checkbox" class="group" chk="g1"/> Pilih Semua</b></td>
<td><b><input type="checkbox" class="group" chk="g2"/> Pilih Semua</b></td>
<td><b><input type="checkbox" class="group" chk="g3"/> Pilih Semua</b></td>
</tr>
<tr>
<td>
<input type="checkbox" class="g1"/>
<input type="checkbox" class="g1"/>
<input type="checkbox" class="g1"/>
</td>
<td>
<input type="checkbox" class="g2"/>
<input type="checkbox" class="g2"/>
<input type="checkbox" class="g2"/>
</td>
<td>
<input type="checkbox" class="g3"/>
<input type="checkbox" class="g3"/>
<input type="checkbox" class="g3"/>
</td>
</tr>
</table>
the script: Jquery
$(function(){
$(".group").click(function(){
var click = $(this).attr('chk');
var current = $("."+click).attr('checked');
if(current){
$("."+click).removeAttr('checked');
}else{
$("."+click).attr('checked','checked');
}
})
})
check this out real simulation