Refresh and update data after change on cell - Please Help

Refresh and update data after change on cell - Please Help

bilusesbiluses Posts: 26Questions: 10Answers: 0

I have data Revenue and data Platform_revenue and data Difference.

Revenue is allways with data from 0 to n
Platform_revenue is set to 0 on Mysql when new entry is created.
When you change Platform_Revenue i want to make= Platform_revenue - Revenue = Difference call an ajax to update Difference on Mysql and refresh that row.

Actually what i have is this, if Revenue AND Platform_revenue is >= 0 ( so data is changed) y make the simple subtraction, save on variable and UPDATE the MySQL with AJAX with the new Difference.

That obviously is not the way, because i have to Refres the browser Twice to make changes appear on client side.
Here is my VERY BAD code, but i don't know how to make it the way i explained.

$('#example').DataTable({
//autoset difference
if ( ((data.incomes.revenue.replace(/[\$,]/g, '') * 1 >= 0)&&(data.incomes.platform_revenue.replace(/[\$,]/g, '') * 1 >= 0)) )
{
var revenueValue = data.incomes.revenue.replace(/[\$,]/g, '') * 1;
var platformValue = data.incomes.platform_revenue.replace(/[\$,]/g, '') * 1;
var datetime = data.incomes.date;
var advertiser = data.incomes.advertiser;
var result = platformValue - revenueValue;
$.ajax({
type: "POST",
url: "php/diff_update.php",
data: {
advid: advertiser,
rev: revenueValue,
date: datetime,
diff: result
},
success: function(data) {
console.log('success');
var tableRefresh = $('#example').DataTable();
var rows = tableRefresh
.draw();
}
});
}
}

ALSO the diff_update.php who is working perfectly.

<?php //require_once('include/util.php'); $advid = $_POST['advid']; $rev = $_POST['rev']; $date = $_POST['date']; $diff = $_POST['diff']; include('../conect.php'); $sql="UPDATE `incomes` SET `difference`='$diff' where advertiser='$advid' AND revenue='$rev' AND date='$date'"; mysqli_query($con,$sql); mysqli_close($con); ?>

please help me.

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    If I understand you correctly, you are calculating the difference between column A and column B and saving the result to the database.

    Do you really need the difference saved to the db? My approach would be to calculate the difference with a column.render function, for display only.

  • bilusesbiluses Posts: 26Questions: 10Answers: 0

    @tangerine yes i need to save the data on database.

    Yes i calculate A-B = Difference. Need to save difference on database and update the view for the user.

    Actually as you can see what i have is on createdRow function, i check all the entries for Data B who are > 0 ( that means A - B is possible to do, because initially B is = 0) and make the Ajax update. But i need to reload the browser.

    I need that when Cell B change, make the A-B and save the difference on database and refresh view.

  • bilusesbiluses Posts: 26Questions: 10Answers: 0

    Any help please?

  • bilusesbiluses Posts: 26Questions: 10Answers: 0
    edited April 2017

    please someone knows how to update the table after a change happens? thanks!

This discussion has been closed.