javascript 使用javascript更新sql记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14617667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:07:31  来源:igfitidea点击:

update sql record using javascript

phpjavascriptforms

提问by Renee Cribe

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

调用这个页面来处理 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?

它适用于单笔交易。但是我需要稍微编辑一下,因为我现在正在表中显示我的数据。我现在有这个 js 代码,当我在我的每行表上点击一个 btn,传递我的 row_id 和 currentbal calue,我得到了它的工作,但我想从这个 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");
    }   
}

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

js代码目前只有两个变量

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

row_id= 这基本上是 db 行的 ID, currentbal= 这是我想上传到我的 db 的值

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

我的问题基本上是我如何调用我的firstbillsubmitbal.php文件以及我如何编辑我的 php 文件,以便我的 row_id 和currentbal我的数据库上传,因为我不再使用 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.

感谢您的答复。所以我浏览了一些 SO 答案和我在 google 上找到的一些教程,这就是我的 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>") 
        } 

    });             
}

And this is what happened to my firstbillsubmitbal.php page

这就是我的 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());

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.

另外,这是我调用函数的方式。#COL1# 是我的行 ID 值。

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

回答by sourcecode

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

您的函数是否被 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>") 
        } 

    });             
}

and in php file

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

回答by aimadnet

HTML :

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 :

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!

希望对你有帮助!