Forum Moderators: open
<?php
$color1 = "even";
$color2 = "odd";
$row_count = 0;
while($row=mysql_fetch_array($result1)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
$id = $row['weighbridge_id'];
$ref = $row['weighbridge_ref'];
$in = $row['weighbridge_in'];
$out = $row['weighbridge_out'];
$receipt = $row['weighbridge_receipt'];
?>
<tr id="<?php echo $id; ?>" class="edit_tr <?php echo $row_color; ?>">
<td class="edit_td"><span id="ref_<?php echo $id; ?>" class="text"><?php echo $ref; ?></span>
<input type="text" value="<?php echo $ref; ?>" class="editbox w140" id="ref_input_<?php echo $id; ?>" /></td>
<td class="edit_td"><span id="in_<?php echo $id; ?>" class="text"><?php if ($in == 0) { echo " "; } else { echo "£" . $in; } ?></span>
<input type="text" value="<?php echo $in; ?>" class="editbox w80" id="in_input_<?php echo $id; ?>" /></td>
<td class="edit_td"><span id="out_<?php echo $id; ?>" class="text"><?php if ($out == 0) { echo " "; } else { echo "£" . $out; } ?></span>
<input type="text" value="<?php echo $out; ?>" class="editbox w80" id="out_input_<?php echo $id; ?>" /></td>
<td class="edit_td"><span id="receipt_<?php echo $id; ?>" class="text"><?php echo $receipt; ?></span>
<input type="text" value="<?php echo $receipt; ?>" class="editbox w140" id="receipt_input_<?php echo $id; ?>" /></td>
</tr>
<?php $row_count++; }?>
<script type="text/javascript">
$(document).ready(function()
{
$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#ref_"+ID).hide();
$("#in_"+ID).hide();
$("#out_"+ID).hide();
$("#receipt_"+ID).hide();
$("#ref_input_"+ID).show();
$("#in_input_"+ID).show();
$("#out_input_"+ID).show();
$("#receipt_input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var ref=$("#ref_input_"+ID).val();
var inn=$("#in_input_"+ID).val();
var out=$("#out_input_"+ID).val();
var receipt=$("#receipt_input_"+ID).val();
var dataString = 'id='+ ID +'&in='+inn+'&out='+out+'$receipt='+receipt;
//$("#ref_"+ID).html('<img src="load.gif" />'); // Loading image
if(ref.length>0)
{
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
//window.location.replace("http://www.domain.com"); JUST TESTING
$("#ref_"+ID).html(ref);
$("#in_"+ID).html(inn);
$("#out_"+ID).html(out);
$("#receipt_"+ID).html(receipt);
}
});
}
else
{
alert('Enter something.');
}
});
// Edit input box click action
$(".editbox").mouseup(function()
{
return false
});
// Outside click action
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});
});
</script>
<?php
include ('../includes/global.php');
if($_POST['id'])
{
$id=mysql_escape_string($_POST['id']);
$ref=mysql_escape_string($_POST['ref']);
$in=mysql_escape_string($_POST['in']);
$out=mysql_escape_string($_POST['out']);
$receipt=mysql_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighrbidge_ref='$ref', weighbridge_in='$in', weighbridge_out='$out', weighbridge_receipt=$receipt WHERE weighbridge_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
}
?>
var dataString = 'id='+ ID +'&in='+inn+'&out='+out+'$receipt='+receipt;
var dataString = 'id='+ ID +'&in='+inn+'&out='+out+'&receipt='+receipt;
<?php
include ('../includes/global.php');
if($_POST['id'])
{
$id=mysql_escape_string($_POST['id']);
$ref=mysql_escape_string($_POST['ref']);
$in=mysql_escape_string($_POST['in']);
$out=mysql_escape_string($_POST['out']);
$receipt=mysql_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighrbidge_ref='$ref', weighbridge_in='$in', weighbridge_out='$out', weighbridge_receipt=$receipt WHERE weighbridge_id='$id'";
echo $sql;
$result = mysql_query($sql) or die(mysql_error());
}
else
{
echo "Missing id";
}
?>
success: function(html)
{
alert(html);
//window.location.replace("http://www.domain.com"); JUST TESTING
$("#ref_"+ID).html(ref);
$("#in_"+ID).html(inn);
$("#out_"+ID).html(out);
$("#receipt_"+ID).html(receipt);
}
The balance doesn't update until the page is refreshed.
<tr>
<?php
$balance_sql="SELECT SUM(weighbridge_in) AS invalue, SUM(weighbridge_out) AS outvalue FROM weighbridge WHERE weighbridge_date >= DATE(NOW())";
$balance_result = mysql_query($balance_sql) or die(mysql_error());
while($balance_row=mysql_fetch_array($balance_result)) {
$sum_in = $balance_row['invalue'];
$sum_out = $balance_row['outvalue'];
$balance = $sum_in - $sum_out;
$class = (($balance < 1000) ? 'red' : (($balance < 3000) ? 'orange' : 'green' ))
?>
<td colspan="4"><h1 class="<?php echo $class ?>">Balance: £<?php echo number_format($balance,2); ?></h1></td>
</tr>
<?php } ?>
<?php
include ('../includes/global.php');
if($_POST['id'])
{
$id=mysql_escape_string($_POST['id']);
$ref=mysql_escape_string($_POST['ref']);
$in=mysql_escape_string($_POST['in']);
$out=mysql_escape_string($_POST['out']);
$receipt=mysql_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighrbidge_ref='$ref', weighbridge_in='$in', weighbridge_out='$out', weighbridge_receipt=$receipt WHERE weighbridge_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
$balance_sql="SELECT SUM(weighbridge_in) AS invalue, SUM(weighbridge_out) AS outvalue FROM weighbridge WHERE weighbridge_date >= DATE(NOW())";
$balance_result = mysql_query($balance_sql) or die(mysql_error());
while($balance_row=mysql_fetch_array($balance_result)) {
$sum_in = $balance_row['invalue'];
$sum_out = $balance_row['outvalue'];
$balance = $sum_in - $sum_out;
$class = (($balance < 1000) ? 'red' : (($balance < 3000) ? 'orange' : 'green' ))
echo "{balance: '" . number_format($balance,2) . "',class: '" . $class . "'}";
}
else
{
echo "{error: 'Missing id'}";
}
?>
<?php
include ('../includes/global.php');
if($_POST['id']) {
$id=mysql_real_escape_string($_POST['id']);
$ref=mysql_real_escape_string($_POST['ref']);
$in=mysql_real_escape_string($_POST['in']);
$out=mysql_real_escape_string($_POST['out']);
$receipt=mysql_real_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighbridge_ref='$ref', weighbridge_in='$in', weighbridge_out='$out', weighbridge_receipt='$receipt' WHERE weighbridge_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
$balance_sql="SELECT SUM(weighbridge_in) AS invalue, SUM(weighbridge_out) AS outvalue FROM weighbridge WHERE weighbridge_date >= DATE(NOW())";
$balance_result = mysql_query($balance_sql) or die(mysql_error());
while($balance_row=mysql_fetch_array($balance_result)) {
$sum_in = $balance_row['invalue'];
$sum_out = $balance_row['outvalue'];
$balance = $sum_in - $sum_out;
$class = (($balance < 1000) ? 'red' : (($balance < 3000) ? 'orange' : 'green' ))
echo "{balance: '" . number_format($balance,2) . "', class: '" . $class . "'}";
}
else
{
echo "{error: 'Missing id'}";
}
?>
$arr = array("balance" => number_format($balance,2), "class" => $class);
echo json_encode($arr);
$arr = array("error" => "Missing id");
echo json_encode($arr);
<?php
include ('../includes/global.php');
if($_POST['id']) {
$id=mysql_real_escape_string($_POST['id']);
$ref=mysql_real_escape_string($_POST['ref']);
$in=mysql_real_escape_string($_POST['in']);
$out=mysql_real_escape_string($_POST['out']);
$receipt=mysql_real_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighbridge_ref='$ref', weighbridge_in='$in', weighbridge_out='$out', weighbridge_receipt='$receipt' WHERE weighbridge_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
$balance_sql="SELECT SUM(weighbridge_in) AS invalue, SUM(weighbridge_out) AS outvalue FROM weighbridge WHERE weighbridge_date >= DATE(NOW())";
$balance_result = mysql_query($balance_sql) or die(mysql_error());
while($balance_row=mysql_fetch_array($balance_result)) {
$sum_in = $balance_row['invalue'];
$sum_out = $balance_row['outvalue'];
$balance = $sum_in - $sum_out;
$class = (($balance < 1000) ? 'red' : (($balance < 3000) ? 'orange' : 'green' ));
echo "{balance: '" . number_format($balance,2) . "', class: '" . $class . "'}";
}}
//else
//{
//echo "{error: 'Missing id'}";
//}}
?>
<?php
include ('../includes/global.php');
if($_POST['id']) {
$id=mysql_real_escape_string($_POST['id']);
$ref=mysql_real_escape_string($_POST['ref']);
$in=mysql_real_escape_string($_POST['in']);
$out=mysql_real_escape_string($_POST['out']);
$receipt=mysql_real_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighbridge_ref='$ref', weighbridge_in='$in', weighbridge_out=' $out', weighbridge_receipt='$receipt' WHERE weighbridge_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
$balance_sql="SELECT SUM(weighbridge_in) AS invalue, SUM(weighbridge_out) AS outvalue FROM weighbridge WHERE weighbridge_date >= DATE(NOW())";
$balance_result = mysql_query($balance_sql) or die(mysql_error());
while($balance_row=mysql_fetch_array($balance_result)) {
$sum_in = $balance_row['invalue'];
$sum_out = $balance_row['outvalue'];
$balance = $sum_in - $sum_out;
$class = (($balance < 1000) ? 'red' : (($balance < 3000) ? 'orange' : 'green' ))
echo "{balance: '" . number_format($balance,2) . "', class: '" . $class . "'}";
}
// MISSING } HERE!
else
{
echo "{error: 'Missing id'}";
}
?>
<?php
include ('../includes/global.php');
if($_POST['id']) {
$id=mysql_real_escape_string($_POST['id']);
$ref=mysql_real_escape_string($_POST['ref']);
$in=mysql_real_escape_string($_POST['in']);
$out=mysql_real_escape_string($_POST['out']);
$receipt=mysql_real_escape_string($_POST['receipt']);
$sql = "UPDATE weighbridge SET weighbridge_ref='$ref', weighbridge_in='$in', weighbridge_out=' $out', weighbridge_receipt='$receipt' WHERE weighbridge_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
$balance_sql="SELECT SUM(weighbridge_in) AS invalue, SUM(weighbridge_out) AS outvalue FROM weighbridge WHERE weighbridge_date >= DATE(NOW())";
$balance_result = mysql_query($balance_sql) or die(mysql_error());
while($balance_row=mysql_fetch_array($balance_result)) {
$sum_in = $balance_row['invalue'];
$sum_out = $balance_row['outvalue'];
$balance = $sum_in - $sum_out;
// MISSING } HERE!
$class = (($balance < 1000) ? 'red' : (($balance < 3000) ? 'orange' : 'green' ))
echo "{balance: '" . number_format($balance,2) . "', class: '" . $class . "'}";
}
else
{
echo "{error: 'Missing id'}";
}
?>
Then add to your AJAX request:
dataType: "json"
Then in your success handler, check to see if html.error, and if it's undefined/null, then find the <h1> element where you show the balance, update it's class, and update it's innerHTML. You'll probably want to give it an id to make it easy to find.
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
dataType: "json",
cache: false,
success: function(html)
{
if (html.error) {
// There was an error (missing id)
}
else {
//window.location.replace("http://www.domain.com"); JUST TESTING
$("#ref_"+ID).html(ref);
$("#in_"+ID).html(inn);
$("#out_"+ID).html(out);
$("#receipt_"+ID).html(receipt);
$(YOURIDHERE).html(html.balance);
$(YOURIDHERE).addClass(html.class);
}
}
<h1 id="balance" class="<?php echo $class ?>">Balance: £<?php echo number_format($balance,2); ?></h1>
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
//dataType: "json",
cache: false,
success: function(html)
{
if (html.error) {
// There was an error (missing id)
} else {
//alert(html);
//window.location.replace("index2.php");
$("#ref_"+ID).html(ref);
$("#in_"+ID).html(inn);
$("#out_"+ID).html(out);
$("#receipt_"+ID).html(receipt);
$("#balance").html(html.balance);
//$("#balance").addClass(html.class); //SORT AFTER
}
echo "{balance: '" . number_format($balance,2) . "', class: '" . $class . "'}";
$arr = array("balance" => "Balance: £" . number_format($balance,2), "class" => $colour);
$("#in_"+ID).html(inn);
$("#out_"+ID).html(out);
if (html.inn >= 0) {
$("#in_"+ID).html("Right £" + inn);
}
else
{
$("#in_"+ID).html("Wrong " + inn);
}
<script type="text/javascript">
$(document).ready(function()
{
$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#ref_"+ID).hide();
$("#in_"+ID).hide();
$("#out_"+ID).hide();
$("#detail_"+ID).hide();
$("#ref_input_"+ID).show();
$("#in_input_"+ID).show();
$("#out_input_"+ID).show();
$("#detail_input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var ref=$("#ref_input_"+ID).val();
var inn=$("#in_input_"+ID).val();
var out=$("#out_input_"+ID).val();
var detail=$("#detail_input_"+ID).val();
var dataString = 'id='+ ID +'&ref='+ref+'&in='+inn+'&out='+out+'&detail='+detail;
//$("#ref_"+ID).html('<img src="load.gif" />'); // Loading image
if(ref.length>0)
{
alert(dataString);
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
dataType: "json",
cache: false,
success: function(html)
{
if (html.error) { // There was an error (missing id)
}
else
{
//alert(html);
$("#ref_"+ID).html(ref);
if (inn > 0) {
$("#in_"+ID).html("£" + inn);
$("#out_"+ID).html(out);
}
else
{
$("#out_"+ID).html("£" + out);
$("#in_"+ID).html(inn);
}
$("#receipt_"+ID).html(receipt);
$("#balance").html(html.balance);
$("#balance").removeClass('red orange green').addClass(html.colour);
}
}
});
}
else
{
alert('Enter something.');
}
});
$(".editbox").mouseup(function()
{
return false
});
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});
});
</script>
when using "dataType: "json",". If I use alert(html) in the success handler I get [object Object]