Unable to Edit Database Entries in Table on Website - javascript

I am working on a website database system that I am able to edit each of the content listed in the table inline. I just click on it, type to add new data, and then clicking off of it saves the new data to the database. I am able to click on each one of the entries, and begin to type, but it does not save the updated information. I feel like it has an easy solution, but I haven't been able to find it. I have been looking over it for sometime now and cannot seem to figure it out. Any help is much appreciated..
index.php
<?php
include 'db.inc.php';
$sql= "SELECT * FROM crosswalk";
$query = $db->prepare($sql);
$query->execute();
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function activate (element) {
// alert('clicked')
}
function updateValue(element, column, id){
var value = element.innerText
$.ajax({
url:'backend.php',
type: 'post',
data:{
value: value,
column: column,
id: id
},
success:function(php_result){
console.log(php_result);
}
})
}
</script>
<table>
<th>Category</th>
<th>Product Category</th>
<th>NIGP</th>
<th>NIGP Short Description</th>
<th>NIGP Full Description</th>
<th>P-Group</th>
<th>Category Specialist</th>
<th>GL Unit Price < $500</th>
<th>GL Unit Price < $500 - $4,999</th>
<th>GL Unit Price - $5000 +</th>
<?php
while($row = $query->fetch()){
$c = $row['Category'];
$p = $row['Product Category'];
$n = $row['NIGP'];
$s = $row['NIGP Short Description'];
$f = $row['NIGP Full Description'];
$g = $row['P-Group'];
$gl1 = $row['GL Unit Price<$500'];
$gl2 = $row['GL Unit Price<$500-$4,999'];
$gl3 = $row['GL Unit Price-$5000+'];
$id = md5($row['id']);
?>
<tr>
<td><div contenteditable="true" onBlur="updateValue(this, 'Category','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $c; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'Product Category','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $p; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'NIGP','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $n; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'NIGP Short Description','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $s; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'NIGP Full Description','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $f; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'P-Group','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $g; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'GL Unit Price<$500','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $gl1; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'GL Unit Price<$500-$4,999','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $gl2; ?></div></td>
<td><div contenteditable="true" onBlur="updateValue(this, 'GL Unit Price-$5000+','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $gl3; ?></div></td>
</tr>
<?php
}
?>
</table>
backend.php
<?php
include 'db.inc.php';
if(isset($_POST['id'])){
$value = $_POST['value'];
$column = $_POST['column'];
$id = $_POST['id']; //md5
$sql="UPDATE crosswalk SET $column = :value WHERE md5(id) = :id LIMIT 1";
$query = $db->prepare($sql);
$query->bindParam('value',$value);
$query->bindParam('id',$id);
if($query->execute()){
echo "Update Successful";
}else{
echo "Failure";
}
}
?>
Here are the error messages I am getting
Error Pics

Related

Sweetalert won't show but it will update

I want to show a sweetalert after clicking the set button but it won't function. This is my index page and the set button can function but it won't show the sweet alert. what might be the problem and what should I do?
index.php
<form method='post' action='updataStatus.php'>
<button type='submit' name='but_update' class="inline-block float ml-2 mt-1 btn-group pull-right btn-danger btn-sm">SET</button><button type="submit" id="dataExport" name="dataExport" value="Export to excel" class="inline-block float ml-2 mt-1 btn-group pull-right btn-info btn-sm">Export</button>
<div class="table-responsive">
<br>
<tbody><table class="table table-hover table-bordered" id="sampleTable2">
<thead>
<tr>
<th><input type="checkbox" class="select-all checkbox" name="select-all" id="checkAll" /></th>
<th>Name</th>
<th>Scholarship Program</th>
<th>Course</th>
<th>Semester</th>
<th>Allowance</th>
</tr>
</thead>
<?php
require_once "connection.php";
$query = "SELECT * FROM allowance";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result) ){
$id = $row['id'];
$Name = $row['Name'];
$Scholarship = $row['Scholarship'];
$Course = $row['Course'];
$Semester = $row['Semester'];
$statusAllowance = $row['statusAllowance'];
?>
<tr>
<!-- Checkbox -->
<td><input type='checkbox' name='update[]' value='<?= $id ?>' ></td>
<td><p name="Name"><?php echo $row['Name']; ?></p></td>
<td><p name="Scholarship"><?php echo $row['Scholarship'] ?></p></td>
<td><p name="Course"><?php echo $row['Course'] ?></p></td>
<td><p name="Semester"><?php echo $row['Semester'] ?></p></td>
<td><p name='statusAllowance_<?= $id ?>'><?php echo $row['statusAllowance'] ?></td>
</tr>
<
?php
}
?>
</table>
</tbody>
<?php
if(isset($_SESSION['success']) && $_SESSION['success'] !='')
{
?>
<script type="text/javascript">
swal({
title: "<?php echo $_SESSION['success']; ?>",
icon: "<?php echo $_SESSION['status_code']; ?>",
button: "yissh",
});
</script>
<?php
unset($_SESSION['success']);
}
?>
This is my code on the edit part and this works, only the alert won't show up.
updataStatus.php
<?php
require_once "connection.php";
if(isset($_POST['but_update'])){
if(isset($_POST['update'])){
foreach($_POST['update'] as $id){
$statusAllowance = 'Received';
if($statusAllowance != '' ){
$updateUser = "UPDATE allowance SET statusAllowance='".$statusAllowance."' WHERE id=".$id;
$query_run = mysqli_query($conn,$updateUser);
if($query_run){
$_SESSION['success'] = "YOUR DATA UPDATED";
header('Location: tracking.php');
}else{
$_SESSION['success'] = "YOUR DATA IS NOT UPDATED";
header('Location: tracking.php');
}
}
}
}
}
?>
considering you have not implemented another function to call sweetalert; by default, it should be Swal.fire({}) not just swal({})
https://sweetalert2.github.io/

change <td> tag to editable and submit form after editing cakephp 2

I am working in a cakephp 2 project. On my view I have a table on which I want to make a cell editable. After editing I want want to save it.
I have tried contentEditable of HTML 5. By that I can edit but data is not passed to controller. I also tried innerHTML which change the cell to input but I can't write anything on the input field. How can I solve the problem? Thanks
<script>
function makeInput(e) {
document.getElementById('test').innerHTML= "<input type='text'>"
}
function formSubmit2(){
document.getElementById('StockReportEditForm').submit();
}
</script>
<?php if(!empty($results)): ?>
<?php echo $this->Form->create('StockReport',array('url'=>array('type'=>'post','action'=>'edit')));?>
<table>
<tr>
<th>Year Week</th>
<th>Counter</th>
<th>Refrigerator</th>
<th>Freezer</th>
<th>Summary</th>
</tr>
<?php foreach ($results as $result): ?>
<?php $today = date("Y-m-d", strtotime("+1 week"));
$date = new DateTime($today);
$week = $date->format("W");
$week_int = (int)$week;
$week_int=$week_int-1;
$last_week = (string)$week_int;
$year = date("Y"); ?>
<tr>
<?php if($year.$last_week==$result['StockReport']['yearweek']): ?>
<td onclick="makeInput(this)" id="test">
<?php echo $result['StockReport']['yearweek']; ?>
<?php else:?>
<td><?php echo $result['StockReport']['yearweek'];?></td>
<?php endif ?>
</td>
<td><?php echo $result['StockReport']['counter']; ?></td>
<td><?php echo $result['StockReport']['refrigerator']; ?></td>
<td><?php echo $result['StockReport']['freezer']; ?></td>
<td><?php echo $result['StockReport']['summary']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
// echo $this->Form->submit('Submit');
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
),
'id'=>'submitButton',
'onclick'=>'formSubmit2()'
);
echo $this->Form->end($options); ?>
<?php endif; ?>
I would create a hidden input within the td element and when the form is submitted I copy the htmlinner of the contenteditabe to the hidden input value.

Updating quantity in cart table

I want that update button should be in that field which I choose.But it appears only in first row and is updating for first row quantity field.
<form action="cart.php?action=update" method="post">
<table>
<tr>
<th colspan="2">ITEM</th>
<th>QUANTITY</th>
<th>PRICE</th>
<th>SUBTOTAL</th>
<th>REMOVE</th>
</tr>
<?php
$query = "select * from cart where customer_id='$user' ";
$result = mysqli_query($con,$query);$b = 0;$c = 0;
while($row = mysqli_fetch_array($result))
{
$productid = $row['product_id'];
$query2 = "select * from product where product_id='$productid'";
$result2 = mysqli_query($con,$query2);
while($row2=mysqli_fetch_array($result2))
{
?>
<tr>
<td rowspan="3"><img src="upload/<?php echo $row2['pimage']; ?>" height="50px" width="50px"></td>
<td rowspan="3"><?php echo $row2['pname']; ?></td>
<td rowspan="3">
<input tpe="text" name="newqty" value="<?php echo $qty = $row['quantity']; ?>" onkeypress="showsubmit()">
<input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;" type="submit" name="sub1" id="sub1" value="UPDATE">
<input type="hidden" name="hidcartid" value="<?php echo $row['cart_id'] ?>"/>
<input type="hidden" name="hidproductid" value="<?php echo $row['product_id']; ?>"/>
<script>
function showsubmit()
{
document.getElementById("sub1").style.visibility = "visible";
}
</script>
</td>
<td>Price:<?php echo $sp = $row2['psellingprice']; ?></td>
<?php
$total = $sp * $qty;
?>
<td rowspan="3">
<?php
echo $t = $total;
$b = $b + $t;
?></td>
<td rowspan="3">REMOVE</td>
</tr>
<?php
$action = ( array_key_exists( 'action', $_REQUEST) ? $_REQUEST['action'] : "" );
if($action =="delete")
{
deletecart($_REQUEST['cid']);
}
if($action=="update")
{
echo "update function called";
updatecart();
echo "update function executed";
}
?>
<tr>
<td>Selling Price:<?php echo $p = $row2['pprice']; ?></td>
</tr>
<tr>
<td>You Saved:
<?php
$d = $row2['pdiscount'];
$s = ($p*$d)/100;
echo $q = $s * $qty;
$c = $c + $q;
?> rs.</td>
</tr>
<?php
}
}
?>
</table>
and cart2.php
<?php
function deletecart($cartid)
{
include 'connection.php';
$sql1="delete from cart where cart_id=$cartid";
$executequery = mysqli_query($con,$sql1);
header('location:cart.php');
}
function updatecart()
{
include 'connection.php';
$cartId = $_POST['hidcartid'];
$productId = $_POST['hidproductid'];
$newqty = $_POST['newqty'];
echo("inside update function");
// update product quantity
$sql = "UPDATE cart
SET quantity = $newqty
WHERE cart_id = $cartId";
mysqli_query($con,$sql);
header('location:cart.php');
}?>
For row onw everything is fine.But for row2 update button appears in row1 and not updating at all.
<input tpe="text" name="newqty" value="<?php echo $qty = $row['quantity']; ?>" onkeypress="showsubmit(<?php echo $row['cart_id'] ?>)">
<input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;"
type="submit"
name="sub_<?php echo $row['cart_id'] ?>"
id="sub_<?php echo $row['cart_id'] ?>"
value="UPDATE">
<script>
function showsubmit(id)
{
document.getElementById("sub_"+id).style.visibility ="visible";
}
</script>

Onchange pop out error javascript

I try to auto calculate the input without any button. I try to do it with javascript onchange function.However, javascript keep pop out the error and I do not know how to solve it.
This is the error : Uncaught SyntaxError: Unexpected identifier
below is my code :
<!DOCTYPE html>
<html>
<script>
function jsfunction()
{
var x=document.getElementById("qty1");
var y =document.getElementById("price1");
var z =document.getElementById("total1");
alert("You entered: " + y.value);
}
</script>
<body>
<?php
$item=array("A"=>"10","B"=>"11","C"=>"12","D"=>"13");
$var=1;
$total=0;
$gtotal=0;
?>
<table border=1 width='600'
<tr>
<th>No</th>
<th>item</th>
<th>Check</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>";
<?php
foreach($item as $myItem=>$pricetag){
echo "<tr>";
echo "<td>".$var."</td>";
echo "<td>".$myItem."</td>";
$price = "price". $var;
$qty = "qty". $var;
$total = "total". $var;
?>
<td><input type="checkbox" id="chkbox" name="chkbox"/></td>
<td><input type="text" name=<?php $price ?> id=<?php $price ?> size =1/><?php echo $price ?> </td>
<td><input type="text" onchange="jsfunction() name=<?php $qty ?> id=<?php $qty ?> size =1/><?php echo $qty ?></td>
<td><input type="text" name=<?php $total ?> id=<?php $total ?> size =1/></td>
<?php
$var++;
}?>
</table>
</form>
</body>
</html>
Well, there are a few problems:
You never have the closing > of the <table> tag:
<table border=1 width='600' => <table border=1 width='600'>
You have a random "; when the code is not even PHP nor JavaScript:
<th>Total</th>
</tr>";
You are using the PHP tag as an echo tag:
<?php $price ?> => <?php echo $price ?>
You never close the double quotes when calling your JavaScript function:
onchange="jsfunction() => onchange="jsfunction()"
EDIT:
If your name property is NULL, it may cause problems because the id will be set to the name, so you should wrap the name in quotes (Just to be safe you should probably do it with everything including id, size, name, etc.):
name=<?php $price ?> => name="<?php $price ?>"
Among other things, this line has a JavaScript error:
<td><input type="text" onchange="jsfunction() name=<?php $qty ?> id=<?php $qty ?> size =1/><?php echo $qty ?></td>
It should probably be:
<td><input type="text" onchange="jsfunction()" name="<?php echo $qty; ?>" id="<?php echo $qty; ?>" size="1" /><?php echo $qty; ?></td>
You must use the echo function to print out a PHP variable to the HTML.
Also, throughout your HTML form, don't forget to wrap all the input parameter values in quotation marks (" ").
What is the value of $price? You are getting element's by their id, e.g. price1, but if $price is not equal to the string 'price1' it will fail to get an element by that id and your javascript will be calling into a null value.

Cart is updated only once using Jquery and Codeigniter

I am working on Updating Codeigntier Cart with Jquery using Ajax call to update. Here is my jquery function
$(function() {
$('.cart_form select').on('change', function(ev) {
var rowid = $(this).attr('class');
var qty = $(this).val();
var postData_updatecart = {
'rowid' : rowid,
'qty' : qty
};
//posting data to update cart
$.ajax({
url : base_url + 'bookings/update_booking_cart',
type : 'post',
data : postData_updatecart,
beforeSend : function() {
$('#cart_content').html('Updating...');
},
success : function(html) {
//window.location.href = "postproperty.php?type="+suffix+'&page=page1';
$('#cart_content').html(html);
}
});
});
});
I have cart view file as
<div class="cart_form">
<?php echo form_open(base_url().'index.php/bookings/customerdetails', array('class' => 'bookings_form_customer', 'id' => 'bookings_form_customer')); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th style="text-align:left">Item Description</th>
<th style="text-align:left">QTY</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td>
<select name="<?php echo $i.'[qty]'; ?>" class="<?php echo $items['rowid']; ?>">
<?php
for($tt=0; $tt<=10; $tt++)
{
?>
<option value="<?php echo $tt; ?>" <?php if($tt==$items['qty']) echo 'selected="selected"'; ?>><?php echo $tt; ?></option>
<?php
}
?>
</select>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('update_cart', 'Update your Cart'); ?></p>
</form>
</div>
Things are working Nice. But, lets say if I have 2 products, if i select one item's qty from 1 to 2, cart is updated and updated cart is shown using AJAX. But, if I immediately change another item's qty then it doesnt work.
The cart view file is inside class 'cart_form'.
Helping hands are appreciated.
I got an answer. You can refer below link
Ajax Request works only once
OR
Directly follow this
REPLACE jquery code part by
$(function() {
$(document).on( "change", "#bookings_form_customer select", function(ev) {
// your code goes here
});
});

Categories