Sires
Sires

Reputation: 59

Live Update Table entries to database SQL PDO AJAX

i try to make an editable-table on a homepage which automatically save the new entries after you leave the "field" or press enter but something doesnt work. Everthing is fine until i want to save the new entrie to database. I fired the PDO-SQL-Statment and it works.

For me it seems to be the table_edit_ajax.php wasnt work but i dont know why!

Hope some of you guys can help me?

Im using a normal mysql database

Homepagecode:

<Table>

 $getIDBusinessRessources = $dbPDO->geteditableSingleBusinessRessoure();


   foreach($getIDBusinessRessources as $getidBusiness){
   $id =    $getidBusiness['checkid'] ;


       echo '<tr id="'
            . $getidBusiness['checkid']
            . '" '
            . 'class="edit_tr"'
            . '>';

       // Spalte Ressourcenname
       echo  '<td class="edit_td">'
            .'<span id="RessourceName_'
            .$getidBusiness['checkid']
            . '" '
            . 'class="text">'
            .$getidBusiness['Rn']
            .'</span>'
            .'<input type="text" value="'
            .$getidBusiness['Rn'] 
            . '" class="editbox" id="Ressourcname_Input_'
            . $getidBusiness['checkid']
            .'">'   
            .'</td>';

       // Spalte Amount    
       echo  '<td class="edit_td">'
            .'<span id="Amount_'
            .$getidBusiness['checkid']
            . '" '
            . 'class="text">'
            .$getidBusiness['AM']
            .'</span>'
            .'<input type="text" value="'
            .$getidBusiness['AM'] 
            . '" class="editbox" id="Amount_Input_'
            . $getidBusiness['checkid']
            .'" '
            .'</td>';
</tr>
</table>

JS:

$( document ).ready(function() {

$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#RessourceName_"+ID).hide();
$("#Amount_"+ID).hide();
$("#Ressourcname_Input_"+ID).show();
$("#Amount_Input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var first=$("#Ressourcname_Input_"+ID).val();
var last=$("#Amount_Input_"+ID).val();
var dataString = 'id='+ ID +'&RessourceName='+first+'&Amount='+last;
$("#RessourceName_"+ID).html('<img src="img/bearbeiten.png" />'); // Loading image

if(first.length>0&& last.length>0)
{

$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#RessourceName_"+ID).html(first);
$("#Amount_"+ID).html(last);
}
});
}
else
{
alert('Enter something.');
}

});

// Edit input box click action
$(".editbox").mouseup(function() 
{
return false
});

// Outside click action
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});

Ajax Code:

<?php

    include "DBconnection.php";

    if($_POST['id']){

    $id                 = $_POST['checkid'];
    $RessourceName      = $_POST['Rn'];
    $Amount             = $_POST['AM'];
    $dbPDO->UpdateLiveTableEntries($id,$RessourceName, $Amount );


    }
    ?>

and at least the DB-Update code

function UpdateLiveTableEntries($id ,$Ressourcename, $Amount ){

      // echo '<script type="text/javascript"> alert("inside");</script>';
       $stmt = self::$_db->prepare("UPDATE BalancesheetInput SET RessourceName =:ressname , Amount =:menge WHERE ID =:id");
       $stmt->bindParam(":id", $id);
       $stmt->bindParam(":ressname", $Ressourcename);
       $stmt->bindParam(":menge", $Amount);

       $stmt->execute();




   }

Upvotes: 0

Views: 727

Answers (1)

Marc B
Marc B

Reputation: 360872

If you're sending this:

var dataString = 'id='+ ID +'&RessourceName='+first+'&Amount='+last;
                  ^^          ^^^^^^^^^^^^^           ^^^^^^

why are you looking for these?

$id                 = $_POST['checkid'];
                               ^^^^^---you aren't sending a 'checkid'
$RessourceName      = $_POST['Rn'];
                              ^-- you aren't sending an 'Rn'
$Amount             = $_POST['AM'];
                              ^^-- you aren't sending an 'AM'

Upvotes: 1

Related Questions