Renee Cribe
Renee Cribe

Reputation: 325

update sql record using javascript

I have a simple form before:

<form method="post" action="firstbillsubmitbal.php?id=#COL1#"> 
    <input name="currentbal" type="text" id="currentbal" class="input-mini"  /> 
    <input type="submit" id="submit" value="Save" class="btn btn-success" />
</form>

Which calls this page for processing firstbillsubmitbal.php

$dealID = $_GET["id"]; 
$currentbal = mysql_real_escape_string(stripslashes($_POST['currentbal']));
$sql = mysql_query("UPDATE deals SET 
        currentbal = '$currentbal',
        currentbalDone = 'Yes'
        WHERE deals.dealID = '$dealID'") or die (mysql_error());

It was working fine for single transactions. But I need to edit it a bit since I am displaying my data in a table now. I now have this js code when I click on a btn on my table per row, passes my row_id and currentbal calue, I got it working but I would like to know from this js code how do I process my form?

function funcEdit(row_id){  
var currentbal = document.getElementById('currentbal' + row_id).value;
    var r=confirm("Are You Sure You Want To Proceed?");
    if(r==true) {
        alert("Record is saved");
    } else {
        alert("Cancelling Transaction");
    }   
}

The js code has two variables only at the moment which is

row_id = this is basically the ID of the db row and currentbal = which is the value I want to upload to my db

My question basically is how do I call my firstbillsubmitbal.php file and what/how do I edit on my php file so that my row_id and currentbal are uploaded on my db since I am no long using POST.


Thank you for the replies. So I went thru some SO answers and some tutorials I found on google and this is what happened to my js code.

function funcEdit(row_id){  
var currentbal = document.getElementById('currentbal' + row_id).value;
var dealID = row_id;
//organize the data properly
var data = 'currentbal=' + currentbal.val() + '&dealID=' + dealID.val();

    //start the ajax
    $.ajax({
        url: "firstbillsubmitbal.php",  
        type: "GET",
        data: data,     
        cache: false,
        success: function() {  
        $('#message').html("<h2>Current balance has been updated!</h2>") 
        } 

    });             
}

And this is what happened to my firstbillsubmitbal.php page

$dealID = $_GET['dealID'] 
$currentbal = $_GET['currentbal']

$sql = mysql_query("UPDATE deals SET 
        currentbal = '$currentbal',
        currentbalDone = 'Yes'
        WHERE deals.dealID = '$dealID' LIMIT 1") or die (mysql_error());

But nothing happens when I click on the button to call my function. What am I missing?

Also, here is how I call my function. #COL1# is my row ID value.

<a href="javascript: funcEdit(#COL1#);" class="btn btn-success">Update</a>

Upvotes: 0

Views: 20680

Answers (2)

aimadnet
aimadnet

Reputation: 423

HTML :

<form method="post" action="firstbillsubmitbal.php"> 
    <input name="currentbal" type="text" id="currentbal" class="input-mini"  />
    <a href="#" onclick="funcEdit(#COL1#); return false;" class="btn btn-success">Update</a>
</form>

JS :

function funcEdit(row_id){  

    var currentbal = $("#currentbal").val();

    //organize the data properly
    var data = 'currentbal=' + currentbal + '&dealID=' + row_id;

    //start the ajax
    $.ajax({
        url: "firstbillsubmitbal.php",  
        type: "GET",
        data: data,     
        cache: false,
        success: function() {  
            $('#message').html("<h2>Current balance has been updated!</h2>") 
        } 

    });             
}

Hope it works for you!

Upvotes: 0

sourcecode
sourcecode

Reputation: 1802

Are your function getting called correctly with row_id data ?
if so then might be this will give you a trick,

function funcEdit(row_id){  
var currentbal = document.getElementById('currentbal' + row_id).value;
var dealID = row_id;
    //start the ajax
    $.ajax({
        url: "firstbillsubmitbal.php",  
        type: "GET",
          //pass data like this 
        data: {currentbal:currentbal.val(),dealID: dealID.val()},    
        cache: false,
        success: function(data) {  
        if (data=="1")
        $('#message').html("<h2>Current balance has been updated!</h2>") 
        } 

    });             
}

and in php file

$dealID = $_GET['dealID'] 
$currentbal = $_GET['currentbal']

$sql = mysql_query("UPDATE deals SET 
        currentbal = '$currentbal',
        currentbalDone = 'Yes'
        WHERE deals.dealID = '$dealID' LIMIT 1") or die (mysql_error());

echo "1" ; // if update successful
else echo "0" // if update unsuccessful

Upvotes: 2

Related Questions