Fetching data in PHP - php

I have <table> that display data. I have a button if I click, the data will be displayed in #datacontainer. But the problem is only first row in my table is shown.
Here's my code
HTML - here's the table, imagine that I have 4 data in my table (DATABASE) but the first row only shows the data when you click the button.
<table>
<tr>
<th width="15%;">Image</th>
<th width="0">ID</th>
<th width="50%;">Name</th>
<th>Action</th>
</tr>
<?php while ($row = mysqli_fetch_array($accounts)) { ?>
<tr>
<td><img src="images/<?php echo $row['image']; ?>"></td>
<td><?php echo $row['id']; ?><input type="text" id="text_id" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['lastname']; ?>, <?php echo $row['firstname']; ?></td>
<td>
<i class="fab fa-reddit-alien" id="showdatacontainer" onclick="return chk()"></i>
<i class="far fa-edit" style="margin: 0 7% 0 7%;"></i>
<i class="fas fa-trash"></i>
</td>
<td>
</td>
<td>
</td>
</tr>
<?php } ?>
</table>
<div id="datacontainer"></div>
Javascript - and here's my jquery code
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function chk()
{
$("#datacontainer").slideToggle(300);
var id = document.getElementById('text_id').value;
var dataString = 'id='+ id;
$.ajax({
type: "post",
url: "showdata.php",
data:dataString,
cache:false,
success: function(data){
$("#datacontainer").html(data);
}
});
return false;
}
</script>
PHP - and here's my PHP code "showdata.php"
<?php
$db = mysqli_connect('localhost', 'root', '', 'psbr');
if (isset($_POST['id'])) {
$id = $_POST['id'];
$accounts = mysqli_query($db, "SELECT * from accounts where id = '$id'");
}
?>
<table>
<?php while ($row = mysqli_fetch_array($accounts)) { ?>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<tr>
<td><?php echo $row['lastname']; ?></td>
</tr>
<?php } ?>
</table>
<style type="text/css">
.w
{
font-size: 12vh;
}
</style>
please help me out of this problem thankyou! :D

You are grabbing the value by ID, yet there are several elements with the same ID, because you are in a loop, therefore, you will always get the first answer.
<td><?php echo $row['id']; ?><input type="text" id="text_id" value="<?php echo $row['id']; ?>"></td>

Related

Fetching data using PDO

I have <table> that display data. I have a button if I click, the data will be displayed in #datacontainer. But the problem is "error: expects parameter 1" I don't know what that's mean. Please help me out of this problem
Here's my code
HTML - here's the table, imagine that I have 4 data in my table then when I clicked the button, the #datacontainer will show with the data.
<table>
<tr>
<th width="15%;">Image</th>
<th width="0">ID</th>
<th width="50%;">Name</th>
<th>Action</th>
</tr>
<?php while ($row = mysqli_fetch_array($accounts)) { ?>
<tr>
<td><img src="images/<?php echo $row['image']; ?>"></td>
<td><input type="hidden" id="text_id" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['lastname']; ?>, <?php echo $row['firstname']; ?></td>
<td>
<i class="fab fa-reddit-alien" id="showdatacontainer" onclick="return chk()"></i>
<i class="far fa-edit" style="margin: 0 7% 0 7%;"></i>
<i class="fas fa-trash"></i>
</td>
</tr>
<?php } ?>
</table>
<div id="datacontainer"></div>
and here's my script
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function chk()
{
var id = document.getElementById('text_id').value;
var dataString = ':id='+ id;
$.ajax({
type: "post",
url: "showdata.php",
data:dataString,
cache:false,
success: function(data){
$("#datacontainer").html(data);
$("#datacontainer").slideToggle(300);
}
});
return false;
}
and here's my PHP that what I'm saying when I clicked the button. The error shows that "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result" please help me out of this problem. I really don't know what that's problem. Thankyou
<?php
$db = mysqli_connect('localhost', 'root', '', 'psbr');
if (isset($_POST['id'])) {
$id = $_POST['id'];
$accountsdata = mysqli_query($db, "SELECT * FROM accounts where id=:id");
$statement = $db->prepare($accountsdata);
$statement->bindParam(":id", $id);
$statement->execute();
$data = $statement->fetchAll(PDO::FETCH_ARRAY);
}
?>
<table>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<?php while ($row = mysqli_fetch_array($data)) { ?>
<tr>
<td><?php echo $val['lastname']; ?></td>
</tr>
<?php } ?>
</table>
you are mixing mysqli and pdo see here:
</tr>
<?php while ($row = mysqli_fetch_array($data)) { ?>
<tr>
you need to change:
<table>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<?php while ($row = mysqli_fetch_array($data)) { ?>
<tr>
<td><?php echo $val['lastname']; ?></td>
</tr>
<?php } ?>
</table>
With:
<table>
<tr>
<td class="w"><h1>Name</h1></td>
</tr>
<?php foreach ($data as $val) { ?>
<tr>
<td><?php echo $val['lastname']; ?></td>
</tr>
<?php } ?>
</table>
And you are passing wrong dataString in your ajax code.
Change:
var id = document.getElementById('text_id').value;
var dataString = ':id='+ id;
$.ajax({
type: "post",
url: "showdata.php",
data:dataString,
cache:false,
success: function(data){
$("#datacontainer").html(data);
$("#datacontainer").slideToggle(300);
With
var id = document.getElementById('text_id').value;
$.ajax({
type: "post",
url: "showdata.php",
data:{'id':id},
cache:false,
success: function(data){
$("#datacontainer").html(data);
$("#datacontainer").slideToggle(300);

Delete SQL Record From Within jQuery Dialog

I have a table generated from a php while loop displaying records in a file called userlist.php. Using data-id, each table now has its SQL record id associated with it.
In my container.php file, I have my initial jQuery code which loads userlist.php into a div on a 1 second interval. Now, when clicking each separate table, I hide userlist.php and show another file in a div called userdetails.php, which contains more information about each user.
Now, I am trying to use a jQuery dialog window to show a confirmation message before a record is deleted. In userdetails.php you can see an ajax snippet which I have modified based on some suggestions. The problem is that inserting this into the file prevents each table to be clicked at all to show the details. If I remove it, details can be accessed.
Here is my code without the ajax snippet, including it does not allow accessing the details:
Click Here
container.php
<div id="userlist"></div>
<div id="userdetails"></div>
<script>
setInterval(function(){
$("#userlist").load("userlist.php");
}, 1000);
$('#userdetails').hide();
$(document).on("click",".user", function() {
var id = $(this).attr("data-id");
$.get("userdetails.php", {id: id}).done(function(data) {
$('#userdetails').html(data);
$('#userdetails').show();
$('#userlist').hide();
});
})
$(document).on("click","#back", function() {
$('#userlist').show();
$('#userdetails').hide();
});
</script>
userlist.php
<?php
include 'dbh.php';
$result = $conn->query("SELECT * FROM users");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$color = array("ADMIN"=>"#ebc45b", "MOD"=>"#8fce61", "USER"=>"#9b9ed2");
?>
<table data-id="<?php echo $row['id']; ?>" class="user" title="User ID: <?php echo $row['id']; ?>">
<tr>
<td align="left">User ID:</td>
<td align="right"><?php echo $row['id']; ?></td>
</tr>
<tr>
<td align="left"><?php echo $row['address']; ?></td>
<td align="right"><?php echo $row['zip']; ?></td>
</tr>
<tr>
<td align="left"><?php echo $row['city']; ?></td>
<td align="right"><?php echo $row['state']; ?></td>
</tr>
<tr>
<td align="left"><span style="color: <?php echo $color[$row['user_level']]; ?>"><?php echo $row['user_level']; ?></span></td>
<td align="right">"member since..."</td>
</tr>
</table>
<?php
}
}
?>
userdetails.php
<?php
include 'dbh.php';
$result = $conn->query("SELECT * FROM users WHERE id=" . $_GET["id"]);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="menu">
<span id="back">BACK</span>
<span id="delete" data-id="<?php echo $row['id']; ?>">DELETE</span>
<span id="new">NEW</span>
<span id="edit">EDIT</span>
</div>
<table class="userdetails">
<tr>
<td align="left">First Name:</td>
<td align="right"><?php echo $row['first_name']; ?></td>
</tr>
<tr>
<td align="left">Last Name:</td>
<td align="right"><?php echo $row['last_name']; ?></td>
</tr>
<tr>
<td align="left">Age:</td>
<td align="right"><?php echo $row['age']; ?></td>
<tr>
</tr>
<td align="left">Sex:</td>
<td align="right"><?php echo $row['sex']; ?></td>
</tr>
</table>
<script>
//JQ Delete
$(document).on("click","#delete", function() {
var id = $(this).attr('data-id');
$("#dialog-confirm").data('del-id', id).dialog('open').html('Delete user ' + id + '?');
});
$("#dialog-confirm").dialog({
resizable: false,
title: 'Confirm Delete',
height:150,
modal: true,
autoOpen:false,
buttons: {
'Yes': function() {
var id = $(this).data('del-id');
$.ajax({
type:'POST',
url:'deleteuser.php',
data:{id: id},
}),
$('#append').append('Deleted User: ' + id + '<br>');
$(this).dialog('close');
},
'No': function() {
$(this).dialog('close');
}
}
});
</script>
<div id="dialog-confirm" style="display:none;"></div>
<span id="append"></span>
<?php
}
}
?>
deleteuser.php
<?php
include 'dbh.php';
$sql = "DELETE FROM users WHERE id=" . $_POST["id"];
if ($conn->query($sql) === TRUE) {
echo "User has been deleted!!!";
} else {
echo "Error deleting user: " . $conn->error;
}
$conn->close();
Just looking for a little help, thank you!
Finally got this working with:
$.ajax({
type:'POST',
url:'deleteuser.php',
data:{id: id},
}),
I think that this is what you want:
$.ajax({
url: "deleteuser.php",
type: "post",
data: 'deleteuser.php?id=' + id,
cache: false,
success: function(data) {
// Close your dialogue on success
}
});
Hope this helps!

Value inserting on last row

Shown in the image above, I have two entries on my stock_transfer table.
As an example, I will insert two different values to show you that it's working. (5 stocks on Product 1 and 10 stocks on Product 2)
Now, when I try to insert the stocks requested remaining on Product 1 and leave the Product 2, it will be successful.
Here comes the problem. When I try to insert value (290 stocks) on my Product 2, it will insert the value in the Product 1 table.
Can anyone help me with this? Here's my query code:
if (isset($_POST['addCart']) && $_POST['addCart']=="Confirm Stock Transfer") {
foreach($_POST['qtyBuy'] as $index=>$value){
if($value > 0){
$cartProd_id = $_POST['product_id'][$index];
$cartProd_name = $_POST['product_name'][$index];
$cartProd_date = $_POST['product_exp'][$index];
$cartProd_desc = $_POST['product_desc'][$index];
$cartProd_transid = $_POST['transfer_id'][$index];
//Update stock_transfer requests
$update_stock = "UPDATE stock_transfer SET stocks_transferred = stocks_transferred + $value WHERE transfer_id = $cartProd_transid;";
$update_stockE = mysqli_query($connection, $update_stock);
Here's is my code:
<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Description</th>
<th>Sell Price</th>
<th>Expiry Date</th>
<th>Instock</th>
<th>Stocks Requested Remaining</th>
<th>Stocks Transferred</th>
<th>Quantity to Transfer</th>
</tr>
</thead>
<tbody>
<?php
$getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
$getReqE = mysqli_query($connection, $getReq);
while ($row = mysqli_fetch_array($getReqE)) {
$transfer_id = $row['transfer_id'];
$transfer_number = $row['transfer_number'];
$transfer_status = $row['transfer_status'];
$transfer_frombranch = $row['transfer_frombranch'];
$transfer_tobranch = $row['transfer_tobranch'];
$transfer_product_id = $row['transfer_product_id'];
$transfer_quantity = $row['transfer_quantity'];
$transfer_date = $row['transfer_date'];
$stocks_transferred = $row['stocks_transferred'];
$remainingStocks = $transfer_quantity - $stocks_transferred;
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_price = $row['sell_price'];
$description = $row['description'];
$quantity = $row['quantity'];
$expiry_date = $row['expiry_date'];
echo $transfer_id;
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
<input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
</td>
<input type="hidden" name="transfer_id[]" value="<?php echo $transfer_id; ?>">
<td><?php echo $product_name; ?>
<input type="hidden" name="product_name[]" value="<?php echo $product_name; ?>">
</td>
<td><?php echo $description; ?>
<input type="hidden" name="product_desc[]" value="<?php echo $description; ?>">
</td>
<td>₱ <?php echo number_format($product_price, 2); ?></td>
<td><?php echo $expiry_date; ?>
<input type="hidden" name="product_exp[]" value="<?php echo $expiry_date; ?>">
</td>
<td><strong><?php echo $quantity; ?></strong></td>
<td><?php echo $remainingStocks; ?></td>
<td><?php echo $stocks_transferred; ?></td>
<td>
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
Back to Request List
</div>
Tags
Here's my schema:
See this:
<td>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>" <?php echo ($remainingStocks == 0') ? 'Disabled=true' : '';?> value="0">
</td>
for security issues, I advise you to use ajax and submit just the qty input for one row each time:
<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Description</th>
<th>Sell Price</th>
<th>Expiry Date</th>
<th>Instock</th>
<th>Stocks Requested Remaining</th>
<th>Stocks Transferred</th>
<th>Quantity to Transfer</th>
</tr>
</thead>
<tbody>
<?php
$getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
$getReqE = mysqli_query($connection, $getReq);
while ($row = mysqli_fetch_array($getReqE)) {
$transfer_id = $row['transfer_id'];
$transfer_number = $row['transfer_number'];
$transfer_status = $row['transfer_status'];
$transfer_frombranch = $row['transfer_frombranch'];
$transfer_tobranch = $row['transfer_tobranch'];
$transfer_product_id = $row['transfer_product_id'];
$transfer_quantity = $row['transfer_quantity'];
$transfer_date = $row['transfer_date'];
$stocks_transferred = $row['stocks_transferred'];
$remainingStocks = $transfer_quantity - $stocks_transferred;
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_price = $row['sell_price'];
$description = $row['description'];
$quantity = $row['quantity'];
$expiry_date = $row['expiry_date'];
echo $transfer_id;
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
</td>
<td><?php echo $product_name; ?>
</td>
<td><?php echo $description; ?>
</td>
<td>₱ <?php echo number_format($product_price, 2); ?></td>
<td><?php echo $expiry_date; ?>
</td>
<td><strong><?php echo $quantity; ?></strong></td>
<td><?php echo $remainingStocks; ?></td>
<td><?php echo $stocks_transferred; ?></td>
<td>
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="qty_buy_<?= $transfer_id ?>" min="1" max="<?php echo $remainingStocks;?>"><a class="btn btn-sm add_qty" data-id="<?= $transfer_id ?>"></a>
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
Back to Request List
</div>
<script>
$(function() {
$('.add_qty').click(function() {
var id = $(this).data('id');
var qty = $("#qty_buy_"+id).val();
$.ajax({
type: 'GET',
url:'/your_action',
data: {id : id, qty : qty},
dataType: 'JSON',
}).done(function(json){
// code to update the values in your row
});
});
});
</script>
The problem was on the
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
I think the problem is with your input names. You have assigned arrays as names of your inputs. Try to make them unique using the product id.

JQuery multiple variables sent in url

I have searched for hours now to find out how I could do this, but unfortunately all to no avail.
I am trying to send entered information so it can run through MySQL and obtain the information, then echo it in table on screen.
The issue (as far as I can tell) must be with my JQuery code:
$("#btnCheckNoteIDs").click(function(){
var noteUser = $("#noteUser").val();
var noteDate = $("#noteDate").val();
$("#LoadNoteIDs").load('check_noteIDs.php?noteDate='+noteDate + "&noteUser="+noteUser);
});
My php code is as follows:
$result = mysqli_query($con,"
SELECT ID, ClientID, Note, ToDoDate, CaseID
FROM ToDoNotes
WHERE ToDoStatus='0' and Deleted='0' and `ToDoDate`='".$_GET['noteIDsDate']."' and User='".$_GET['noteIDsDate']."'");
while($row = mysqli_fetch_array($result))
{
$ID = $row['ID'];
$ClientID = $row['ClientID'];
$Note = $row['Note'];
$ToDoDate = $row['ToDoDate'];
$CaseID = $row['CaseID'];
}
?>
<table class="table table-striped">
<thead>
<tr>
<th>Note ID</th>
<th>Client ID</th>
<th>Case ID</th>
<th>Note</th>
<th>To Do Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><? echo $noteID; ?></td>
<td><? echo $ClientID; ?></td>
<td><? echo $CaseID; ?></td>
<td><? echo $Note; ?></td>
<td><? echo $ToDoDate; ?></td>
</tr>
</tbody>
</table>
Can anyone here offer any assistance please? Any help will be greatly appreciated!
Thanks!
Suppose you have form.php like below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" id="btnCheckNoteIDs">
<label> User</label>
<input type="text" name="user" id="noteUser"><br>
<label>Date</label>
<input type="date" name="date" id="noteDate"><br>
<input type="submit" value="submit">
</form>
<br>
<div id="LoadNoteIDs"></div>
</body>
</html>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<script>
$(document ).ready(function() { //make sure document is ready
$('#btnCheckNoteIDs').submit(function(e){
e.preventDefault();//to prevent default behaviour
var noteUser = $("#noteUser").val();
var noteDate = $("#noteDate").val();
if(noteUser==undefined){
alert('No noteuser');
return false;
}else if(noteDate==undefined){
alert('no note date');
return false;//stop sending
}
//to convert %2F, use decodeURIComponent
var param= 'noteDate='+decodeURIComponent(noteDate) + '&noteUser='+noteUser;
$.ajax({
url: "check_noteIDs.php",
data: param,
type: "post",
success: function(data){
$('#LoadNoteIDs').html(data);
}
});
});
});
</script>
in check_noteIDs.php which must be in the same folder where you put the form.php
require_once "dbconfig.php";
$user= isset($_REQUEST['noteUser'])? $_REQUEST['noteUser']:'';
$date = isset($_REQUEST['noteDate'])? date('Y-m-d', strototime($_REQUEST['noteDate'])):'';
//You can validate here.
$result = mysqli_query($con," SELECT ID, ClientID, Note,
ToDoDate, CaseID
FROM `ToDoNotes`
WHERE `ToDoStatus`='0' AND `Deleted`='0'
AND `ToDoDate`='$date'
AND `User`='$user'");
$row = mysqli_fetch_assoc($result);
<table class="table table-striped">
<thead>
<tr>
<th>Note ID</th>
<th>Client ID</th>
<th>Case ID</th>
<th>Note</th>
<th>To Do Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['ClientID']; ?></td>
<td><?php echo $row['CaseID']; ?></td>
<td><?php echo $row['Note']; ?></td>
<td><?php echo $row['ToDoDate']; ?></td>
</tr>
</tbody>
</table>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
I think your issue is the submit button in your form, submits the form before the script executes. If that's the case, you need to update your script as below:
$("#btnCheckNoteIDs").click(function(event){
event.preventDefault();
var noteUser = $("#noteUser").val();
var noteDate = $("#noteDate").val();
$("#LoadNoteIDs").load('check_noteIDs.php?noteDate='+noteDate + "&noteUser="+noteUser);
});

jQuery Datatable: Edit button

I added Edit button to my jQuery Datatable. When the user clicks on this button, the row becomes editable. Updated row should be saved to MySQL DB, but here comes the problem - the updated row is not saved to DB. Firebug does not show any error message. Can someone help me figure out the problem?
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#newspaper-b').dataTable({
"sPaginationType":"full_numbers",
"aaSorting":[[3, "asc"]],
"bJQueryUI":true
});
$(".edit_but").click(function() {
var ID=$(this).attr('id');
$("#first_"+ID).hide();
$("#last_"+ID).hide();
$("#first_input_"+ID).show();
$("#last_input_"+ID).show();
});
$(".edit_tr").change(function() {
var ID=$(this).attr('id');
var first=$("#first_input_"+ID).val();
var last=$("#last_input_"+ID).val();
var dataString = 'flightNum='+ ID +'&from='+first+'&STADate='+last;
$("#first_"+ID).html('<img src="load.gif" />'); // Loading image
if(first.length>0&& last.length>0) {
$.ajax({
type: "POST",
url: "callpage.php?page=tables/edit.php",
data: dataString,
cache: false,
success: function(html) {
$("#first_"+ID).html(first);
$("#last_"+ID).html(last);
}
});
} else
{
alert('All fields must be filled out.');
}
});
// Edit input box click action
$(".editbox").mouseup(function() {
return false
});
// Outside click action
$(document).mouseup(function() {
$(".editbox").hide();
$(".text").show();
});
$("tr").click(function(){
$(this).addClass("selected").siblings().removeClass("selected");
});
});
</script>
<table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
<thead>
<tr>
<th scope="col">Flt Num</th>
<th scope="col">From</th>
<th scope="col">STA Date</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row):
$flightNum=$row['flightNum'];
$from=$row['frm'];
$STADate=$row['STADate'];
?>
<tr id="<?php echo $flightNum; ?>" class="edit_tr">
<td><?php echo $row['flightNum'];?></td>
<td class="edit_td">
<span id="first_<?php echo $flightNum; ?>" class="text">
<?php echo $from;?>
</span>
<input type="text" value="<?php echo $from;?>"
class="editbox" id="first_input_<?php echo $flightNum; ?>"/>
</td>
<td class="edit_td">
<span id="last_<?php echo $flightNum; ?>" class="text">
<?php echo $STADate; ?>
</span>
<input type="text" value="<?php echo $STADate; ?>"
class="editbox" id="last_input_<?php echo $flightNum; ?>"/>
</td>
<td class="edit_td"><?php echo $row['pkID']; ?></td>
<td id="<?php echo $flightNum; ?>" class="edit_but">
<div>
<img src='images/edit.png' alt='Edit' />
</div>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
edit.php
<?php
include_once 'include/DatabaseConnector.php';
if(isset($_POST['flightNum'])) {
$flightnum=$_POST['flightNum'];
$from=$_POST['from'];
$STADate=$_POST['STADate'];
$query = 'UPDATE flightschedule
SET frm="'.$from.'",STADate="'.$STADate.'"
WHERE flightNum="'.$flightNum.'"';
DatabaseConnector::ExecuteQuery($query);
echo '1';
} else {
echo '0';
}
?>
Try $(".edit_tr input").change() as the trigger.
Change events are limited to inputs, selects and textareas only, but you've tried binding it to a table row. The code above will bind it to any inputs inside the row with the class.

Categories