jQuery only gets the value of the last row - php

I'm retreiving the "In Stock" data using MySQL
<table class="table table-striped table-bordered table-hover results table-fixed">
<thead>
<tr>
<th class="text-center">#</th>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>In Stock</th>
<th style="width: 20%">Quantity</th>
</tr>
<tr class="warning no-result">
<td colspan="8"><i class="fa fa-warning"></i> No Product Found</td>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM products";
$exec = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($exec)) {
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$description = $row['description'];
$product_quantity = $row['quantity'];
$product_price = $row['sell_price'];
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
<input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
</td>
<td><?php echo $product_name; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $product_price; ?></td>
<td><input type="number" value="<?php echo $product_quantity; ?>" id="qtyResult" disabled></td>
<td><input type="number" name="qtyBuy[]" id="qtyBuy" onkeyup="updateStock()"></td>
</tr>
<?php } ?>
</tbody>
</table>
I want to update the In Stock field automatically when I type a number on the quantity field.
The "San Mig Light's" stock is 500 but when I type a number on quantity field, the stock changes to the last record on the table which is 45.
The rest of the row doesn't also work except for the first one.
Here is my jQuery script.
<script>
function updateStock() {
var inputQty = $('#qtyBuy').val();
var inStock = "<?php echo $product_quantity; ?>"
$('#qtyResult').val(inStock - inputQty);
}
</script>
Table

Try to pass to your function the this and event keywords like:
<td><input type="number" name="qtyBuy[]" id="qtyBuy" onkeyup="updateStock(this, event)"></td>
Because the ID must be unique, try to appent a number like: qtyBuy1, qtyBuy2...
And to select these fields you can: find('input[id^="qtyResult"]') : select input field where the id starts with the string "qtyResult".
So, in your updateStock you can use the parameters to refers to the current values like:
function updateStock(obj, event) {
var inputQty = obj.value;
var inStock = "<?php echo $product_quantity; ?>";
$(obj).closest('tr').find('input[id^="qtyResult"]').val(inStock - inputQty);
}
The snippet:
function updateStock(obj, event) {
var inputQty = obj.value;
var inStock = $(obj).closest('tr').find('input[id^="qtyResult"]').val();
var av = $(obj).closest('tr').find('input[id^="qtyResult"]').data('savedValue');
if (av == undefined) {
$(obj).closest('tr').find('input[id^="qtyResult"]').data('savedValue', inStock);
av = inStock;
}
inStock = av;
console.log(av + ' / ' + inStock + ' / ' + inputQty);
$(obj).closest('tr').find('input[id^="qtyResult"]').val(inStock - inputQty);
}
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<table class="table table-striped table-bordered table-hover results table-fixed">
<thead>
<tr>
<th class="text-center">#</th>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>In Stock</th>
<th style="width: 20%">Quantity</th>
</tr>
<tr class="warning no-result">
<td colspan="8"><i class="fa fa-warning"></i> No Product Found</td>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center">product_id1
<input type="hidden" name="product_id[]" value="1">
</td>
<td>product name1</td>
<td>description1</td>
<td>product_price1</td>
<td><input type="number" value="1" id="qtyResult1" disabled></td>
<td><input type="number" name="qtyBuy[]" id="qtyBuy1" onkeyup="updateStock(this, event)"></td>
</tr>
<tr>
<td class="text-center">product_id2
<input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
</td>
<td>product_name2</td>
<td>description2</td>
<td>product_price2</td>
<td><input type="number" value="1" id="qtyResult2" disabled></td>
<td><input type="number" name="qtyBuy[]" id="qtyBuy2" onkeyup="updateStock(this, event)"></td>
</tr>
</tbody>
</table>

jQuery 3.4.1
Specify this in your js script to get the correct row.
$('.myclickedclass').click(function() {
var id=$(this).data("mydataid"); // This is the most important line
$.ajax({
url:"myserverfile.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data) {
// Whatever
}
});
});

Related

particular Data not getting inserted in a column

I am facing a unknown problem while I go insert data.Some data getting inserted in same table n same column but except one.
The following is my code.
<html>
<body>
<?php
include("index.php");
?>
<form action="addcenter.php" method="post">
<table align="center" border="9" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow">
<h1>Add Your Center Details</h1></td>
</tr>
<tr>
<th>District Name</th>
<td><input type="text" name="district"></td>
</tr>
<tr>
<th>Phone No.</th>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<th>Person's Name</th>
<td><input type="text" name="person"></td>
</tr>
<tr>
<th>Designation</th>
<td><input type="text" name="designation"></td>
</tr>
<tr>
<td align="center" colspan="5"><input type="submit" name="submit"
value="submit"></td>
</tr>
</table>
</form>
<?php
include("includes/connect.php");
if(isset($_POST['submit']))
{
$district=$_POST['district'];
$phone=$_POST['phone'];
$person=$_POST['person'];
$designation=$_POST['designation'];
if($district=='' or $phone=='' or $person=='' or $designation='')
{
echo"<script> alert('Please fill the fiels')</script> ";
exit();
}
$query="insert into centers (District,Phone,ContactPerson,Designation)
values('$district','$phone','$person','$designation') ";
if(mysql_query($query))
{
echo"<center><h1>Details Successfully added to your database</h1>
</center> ";
}
}
?>
<table width=" 900" align="center" border="3">
<tr>
<td align="center" colspan="9" bgcolor="orange"><h1>View all
Centers</h1></td>
</tr>
<tr align="center">
<th>SL No.</th>
<th>District</th>
<th>Phone No</th>
<th>Contact Person</th>
<th>Designation</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$query="select * from centers";
$run=mysql_query($query);
$i=1;
while($row=(mysql_fetch_array($run)))
{
$center_id=$row['center_id'];
$district=$row['District'];
$phone=$row['Phone'];
$contact_person=$row['ContactPerson'];
$designation=$row['Designation'];
?>
<tr align="center">
<td><?php echo $i++ ;?></td>
<td><?php echo $district;?></td>
<td><?php echo $phone ;?></td>
<td><?php echo $contact_person ;?></td>
<td><?php echo $designation ;?></td>
<td>Edit
</td>
<td><input type="button" onclick="deleteme1(<?php echo $center_id ?>)"
name="delete" value="Delete"></td>
</tr>
<script language="javascript">
function deleteme1(delid1)
{
if(confirm("are u sure"))
{
window.location.href='deletecenter.php?del_id1='+delid1+'';
return true;
}
}
</script>
<?php } ?>
</table>
</body>
</html>
The problem is that when I going to insert the Contactperson,district,designation and phone no.Then only the data of Contactperson,district and phone no get inserted but not the designation..I dont know why this is happening even the coding is also right..Please help me. Thankyou
Your "if" statement is assigning '' to $designation. Use '==' for comparison.
if($district=='' or $phone=='' or $person=='' or $designation**=**'')
The if statement says, "if $district is '' or $phone is '' or $person is '' or 'I can assign nothing to $designation (which always succeeds)' - at which point you have successfully assigned '' to $designation. The if statement finishes and you insert '' into designation.

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.

getting data from table to another table when selected

this program works properly i just don't know how to make this additional function work...
dbconfig.php
<?php
$db_host = "localhost";
$db_name = "testproduct";
$db_user = "root";
$db_pass = "";
try{
$db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
$db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
Product Management
Here in product management I will add the product information name,price,stock etc..
add_form.php
<?php
require_once 'dbconfig.php';
if($_POST)
{
$fname = $_POST['name'];
$lname = $_POST['actualprice'];
$contactnum = $_POST['sellprice'];
$email = $_POST['Stock'];
try{
$stmt = $db_con->prepare("INSERT INTO tblproduct(name,actualprice,sellprice,Stock) VALUES(:pname,:pactualprice,:psellprice,:pStock)");
$stmt->bindParam(":pname", $name);
$stmt->bindParam(":pactualprice", $actualprice);
$stmt->bindParam(":psellprice", $sellprice);
$stmt->bindParam(":pStock", $Stock);
if($stmt->execute())
{
echo "Successfully Added";
}
else{
echo "Query Problem";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
<style type="text/css">
#dis{
display:none;
}
</style>
<div id="dis">
</div>
<form method='post' id='emp-SaveForm' action="#">
<table class='table table-bordered'>
<tr>
<td>Product Name</td>
<td><input type='text' name='name' class='form-control' placeholder='EX : john doe' required /></td>
</tr>
<tr>
<td>Actual Price</td>
<td><input type='text' name='actualprice' class='form-control' placeholder='EX : Web Design, App Design' required></td>
</tr>
<tr>
<td>Sell Price</td>
<td><input type='text' name='sellprice' class='form-control' placeholder='EX : 180000' required></td>
</tr>
<tr>
<td>Stock</td>
<td><input type='text' name='Stock' class='form-control' placeholder='EX : john doe' required /></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
<span class="glyphicon glyphicon-plus"></span> Save this Record
</button>
</td>
</tr>
</table>
</form>
index.php
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Product Name</th>
<th>Actual Price</th>
<th>Sell Price</th>
<th>Stock</th>
<th>edit</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<?php
require_once 'dbconfig.php';
$stmt = $db_con->prepare("SELECT * FROM tblproduct ORDER BY id DESC");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['actualprice']; ?></td>
<td><?php echo $row['sellprice']; ?></td>
<td><?php echo $row['Stock']; ?></td>
<td align="center">
<a id="<?php echo $row['id']; ?>" class="edit-link" href="#" title="Edit">
<img src="edit.png" width="20px" />
</a></td>
<td align="center"><a id="<?php echo $row['id']; ?>" class="delete-link" href="#" title="Delete">
<img src="delete.png" width="20px" />
</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Sales Management
here is the problem in sales I will add a sales record which will require a product name. the product will be selected from the table of product management including the other information for that specific product(actual price,stock,selling price) that will be show on the index
add_form.php
<?php
require_once 'dbconfig.php';
if($_POST)
{
$pname = $_POST['pname'];
$gname = $_POST['gname'];
$saledate = $_POST['saledate'];
$quantity = $_POST['quantity'];
$actualprice = $_POST['actualprice'];
$sellprice = $_POST['sellprice'];
$profit = $_POST['profit'];
$carryO = $_POST['carryO'];
$sells = $_POST['sells'];
$expense = $_POST['expense'];
try{
$stmt = $db_con->prepare("INSERT INTO tblsales(pname,gname,saledate,quantity,actualprice,sellprice,carryO,sells,expense,profit)
VALUES(:upname,:ugname,:usaledate,:uquantity,:uactualprice,:usellprice,:ucarryO,:usells,:uexpense,:uprofit)");
$stmt->bindParam(":upname", $pname);
$stmt->bindParam(":ugname", $gname);
$stmt->bindParam(":usaledate", $saledate);
$stmt->bindParam(":uquantity", $quantity);
$stmt->bindParam(":uactualprice", $actualprice);
$stmt->bindParam(":usellprice", $sellprice);
$stmt->bindParam(":ucarryO", $carryO);
$stmt->bindParam(":usells", $sells);
$stmt->bindParam(":uexpense", $expense);
$stmt->bindParam(":uprofit", $profit);
if($stmt->execute())
{
echo "Successfully Added";
}
else{
echo "Query Problem";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
<div id="dis">
</div>
<form method='post' id='emp-SaveForm' action="#">
<table class='table table-bordered'>
<tr>
<td>Product Name</td>
<td><input type='text'name='pname' class='form-control' required> </td>
</tr>
<tr>
<td>Guest Name</td>
<td><input type='text' name='gname' class='form-control' required> </td>
</tr>
<tr>
<td>Sale Date</td>
<td><input type='date' name='saledate' class='form-control' required></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type='text' name='quantity' class='form-control' id="quantity" required></td>
</tr>
<tr>
<td>Actual Price</td>
<td>
<input type='text'name ="actualprice" id="aaprice" class='form-control' required></td>
</tr>
<tr>
<td>Selling Price</td>
<td>
<input type='text' name='sellprice' class='form-control' type = "number" id="ssprice" required></td>
</tr>
<tr>
<td>Carry Over</td>
<td><input type='text' name='carryO' class='form-control' required></td>
</tr>
<tr>
<td>Sells</td>
<td><input type='text' name='sells' class='form-control' required></td>
</tr>
<tr>
<td>Expense</td>
<td><input type='text' name='expense' class='form-control' required></td>
</tr>
<tr>
<td>Profit</td>
<td><input name='profit' class='form-control' type = "number" id="profit" required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
<span class="glyphicon glyphicon-plus"></span> Save this Record
</button>
</td>
</tr>
</table>
index.php
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Product Name</th> //selected from product management table
<th>Sale Date</th>//(user input)
<th>Quantity</th>//(user input)
<th>Actual Price</th> //price of the selected product on the management table
<th>Selling Price</th> //price of the selected product on the management table
<th>Carry Over</th> //stock from the selected product on the management table
<th>Sells</th>//selling price of the selected product on the management table * quantity(user input)
<th>Expense</th>
<th>Profit</th>
<th>edit</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<?php
require_once 'dbconfig.php';
if(isset($_POST['months'])){ $months = $_POST['months']; }else { $months='';}
$stmt = $db_con->prepare("SELECT * FROM tblsales WHERE MONTH(saledate) = '".$months."' ");
//this is for the sales management for the monthly view function didn't include the my months select this post already long enough
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['saledate']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['actualprice']; ?></td>
<td><?php echo $row['sellprice']; ?></td>
<td><?php echo $row['carryO']?></td>
<td><?php echo $row['sells']?></td>
<td><?php echo $row['expense']?></td>
<td><?php echo $row['profit']; ?></td>
<td align="center">
<a id="<?php echo $row['id']; ?>" class="edit-link" href="#" title="Edit">
<img src="edit.png" width="20px" />
</a></td>
<td align="center"><a id="<?php echo $row['id']; ?>" class="delete-link" href="#" title="Delete">
<img src="delete.png" width="20px" />
</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Better way by using ajax for this case.
$.ajax({
url: "data.php",//file wich has query select to db table
data: {id:theid},//describe your data here
dataType: 'json',// type of data that will you get (JSON/HTML).
type: 'POST',//sending type (POST/GET)
success: function(data) {
showTable();
}
});

How to display a value within a disabled text input

So the table should really look like this below when the user accesses the page:
Marks Per Answer Total Marks Marks Remaining
(blank text input) 4 4
(blank text input) 6 6
(disabled text input = 4 (same value as under Total Marks)) 4 0
My question is that how can both steps above be solved by changing the jquery below:
$(function() {
//alert("here");
var questions = $('#markstbl td[class*="_ans"]').length;
//disable single entry
for (var i=0;i<=questions;i++){
if($("[class*=q"+i+"_mark]").length ==1){
var t_marks = $("[class*=q"+i+"_ans]").html();
//alert(t_marks);
$("[class*=q"+i+"_mark]").val(t_marks).attr('readonly',true);
//$("[class*=q"+i+"_mark]").attr('readonly',true);
}
}
//find each question set and add listeners
for (var i=0;i<=questions;i++){
$('input[class*="q'+i+'"]').keyup(function(){
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_'));
var tot_marks = $(".q"+questionno+"_ans_org").val();
//alert(tot_marks);
var ans_t=0;
$("[class*=q"+questionno+"_mark]").each(function(){
var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val());
ans_t+=parseInt(num);
});
ans_t=tot_marks-ans_t;
//alert(ans_t);
//var fixedno = tot_marks;
var ans = ans_t;
var answerText='<strong>'+ans+'</strong>';
$(".q"+questionno+"_ans").val(ans);
$(".q"+questionno+"_ans_text").html(answerText);
});
}
});
Below is the dynamic HTML table:
<form id="Marks" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionId);
$prev_ques = '';
foreach($searchQuestionId as $key=>$questionId){
?>
<tr class="questiontd">
<?php
if($questionId != $prev_ques){
?>
<td class="questionnumtd" name="numQuestion" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$questionId?> <input type="hidden" name="q<?php echo$questionId?>_ans_org" class="q<?php echo$questionId?>_ans_org" value="<?php echo$searchMarks[$key]?>"><input type="hidden" name="q<?php echo$questionId?>_ans" class="q<?php echo$questionId?>_ans" value="<?php echo$searchMarks[$key]?>"></td>
<td class="questioncontenttd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$searchQuestionContent[$key]?> </td>
<?php
}
?>
<td class="answertd" name="answers[]"><?php echo$searchAnswer[$key]?></td>
<td class="answermarkstd">
<input class="individualMarks q<?php echo$questionId?>_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<?php
if($questionId != $prev_ques){
?>
<td class="totalmarkstd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$totalMarks[$key]?></td>
<td class="noofmarkstd q<?php echo$questionId?>_ans_text" q_group="1" rowspan="<?php echo$row_span[$questionId]?>"><?php echo"<strong>".$searchMarks[$key]."</strong>"?></td>
<?php
}
?>
</tr>
<?php
$prev_ques = $questionId;
}
?>
</tbody>
</table>
</form>
At the moment in the jquery function I am trying to use the t_marks variable to display the value in the text input but nothing is displayed when I alert t_marks
You can use readonly instead of disabled
Change
$("[class*=q"+i+"_mark]").val(t_marks).attr("disabled","disabled");
To
$("[class*=q"+i+"_mark]").val(t_marks).attr('readonly',true);
Or You can just make it focus out when it get focus
<input type="text" id="myTxt" onfocus="$(this).blur()"/>

sort data by field heading php mysql

I have a database
that have two tables.
The sql query fetching data correctly.
I want to sort fields data by typing table heading in text box and click on go that in table footer.
Many Thanks
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th class="capt" colspan="6">Available Projects</th>
</tr>
<tr>
<th>Select</th>
<th>Project</th>
<th>Crawler</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include(dirname(__file__)."/includes/dbConn.php");
$result = mysql_query("SELECT *, ( SELECT name FROM projects WHERE projects.id = crawlers.pid ) AS pname FROM `crawlers`", $appConn);
while($row = mysql_fetch_array($result))
{
?>
<tr id="crawler_<?php echo $row['id']; ?>">
<td>
<input value="" id="check" type="checkbox">
<input id="crawler_id_<?php echo $row['id']; ?>" type="hidden" value="<?php $row['id']; ?>" /> <!-- crawler id -->
</td>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>username#domain.com</td>
<td>Enabled</td>
<td><a class="edit" href="#">edit</a><a class="add" href="#">run</a><a class="delete" href="#">delete</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="6"> Sort Fields by <input value="type here" id="field" type="text"> Go </th>
</tr>
</tfoot>
</table>
I think, you are looking for ORDER BY
Have a look at the MySQL Documentation about the SELECT syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html

Categories