最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - update sql record using javascript - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question edited Jan 31, 2013 at 3:45 Renee Cribe asked Jan 31, 2013 at 2:49 Renee CribeRenee Cribe 3251 gold badge4 silver badges14 bronze badges 7
  • Look into using ajax – SeanWM Commented Jan 31, 2013 at 2:51
  • The JavaScript code runs in the user's browser. The PHP code runs on the server. The database is on the server, so you need to use PHP code to do that. As @Sean mentioned, you can create a service that can be called using Ajax. – Steve H. Commented Jan 31, 2013 at 2:53
  • I edited my post due to the replies about AJAX. please note I am a beginner. Thanks =) – Renee Cribe Commented Jan 31, 2013 at 3:27
  • Try adding an error handeling to your $.Ajax call.. see this maybe there is an error. – Mortalus Commented Jan 31, 2013 at 3:32
  • 2 Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. – NullPoiиteя Commented Jan 31, 2013 at 3:35
 |  Show 2 more ments

2 Answers 2

Reset to default 2

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

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!

发布评论

评论列表(0)

  1. 暂无评论