使用javascript更新sql记录 [英] update sql record using javascript

查看:104
本文介绍了使用javascript更新sql记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前有一个简单的表格:

<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>

哪个调用此页面来处理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());

对于单笔交易来说效果很好.但由于我现在将数据显示在表中,因此需要对其进行一些编辑.现在,当我在每行的表上单击btn,传递row_id和currentbal提示时,我有了这个js代码,它可以正常工作,但是我想从此js代码中知道如何处理表单?

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");
    }   
}

js代码只有两个变量,即

row_id =这基本上是数据库行的ID, currentbal =这是我要上传到数据库的值

我的问题基本上是如何调用我的firstbillsubmitbal.php文件以及如何/如何在php文件中进行编辑,以使row_id和currentbal上载到我的数据库中,因为我不再使用POST.


感谢您的答复.因此,我浏览了一些在Google上找到的SO答案和一些教程,这就是我的JS代码所发生的事情.

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>") 
        } 

    });             
}

这就是我的firstbillsubmitbal.php页面上发生的事情

$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());

但是当我单击按钮调用函数时,什么也没有发生.我想念什么?

此外,这就是我调用函数的方式. #COL1#是我的行ID值.

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

解决方案

使用row_id数据正确调用了您的函数吗?
如果是这样,那可能会给你一个窍门,

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>") 
        } 

    });             
}

并在php文件中

$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

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>

解决方案

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

这篇关于使用javascript更新sql记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆