使用 Javascript 从 onClick 更新数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20827548/
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-27 19:23:34  来源:igfitidea点击:

Update database from onClick using Javascript

javascriptphpjquerymysqlfunction

提问by adroxx

I am trying to update a database from an onClick procedure inside an anchor.

我正在尝试从锚点内的 onClick 过程更新数据库。

<a name=“fs1” onClick="updateArea1();"></a>

I am struggling with the function for updateArea1as I'm not really sure how I can pass the name "fs1"from the anchor and update the database from a script process.

我正在努力使用updateArea1函数,因为我不确定如何从锚点传递名称“fs1”并从脚本进程更新数据库。

I would essentially like the database to be updated like this: UPDATE row WHERE id = 1 WITH (name_from_anchor)

我基本上希望数据库像这样更新: UPDATE row WHERE id = 1 WITH (name_from_anchor)

However... the variable does not HAVE to come from the name tag. It could be referenced in the parenthesis of the function like this if it's easier.

但是......变量不必来自名称标签。如果更容易的话,可以像这样在函数的括号中引用它。

<a onClick="updateArea1(fs1);"></a>

Any help would be greatly appreciated.

任何帮助将不胜感激。

-- UPDATED --

- 更新 -

LAUNCH.PHP

启动.PHP

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="apple-mobile-web-app-capable" content="yes" />

    <script src="includes/scripts/jquery-2.0.3.min.js"></script>

    <script>
        function updateArea1(el) {
         $.post("includes/db_update.php", $("#console").serialize());
        }
    </script>

</head>

<body>

    <a name="fs1" onClick="updateArea1(this.name);"></a>
    <a name="fs2" onClick="updateArea1(this.name);"></a>

</body>
</html>

DB_UPDATE.PHP

数据库更新文件

<?php

include 'db_connect.php';   

$area1= mysqli_escape_String($con,$_POST[]);

$query = "UPDATE previewState SET selected='".$area1."' WHERE id='1';";

mysqli_query($con,$query);

mysqli_close($con);

?>

回答by charlietfl

If you pass thisas argument can do:

如果您this作为参数传递可以执行以下操作:

<a name="fs1" onClick="updateArea1(this);"></a>

updateArea1(el){
  alert( el.name)
}

回答by Rajaprabhu Aravindasamy

Since you have tagged this question with Jquery, why dont you use a click handler for that element like,

既然你已经用 Jquery 标记了这个问题,你为什么不为那个元素使用一个点击处理程序,比如,

$('a').click(function(e){
  e.preventDefault();
  updateAreat1($(this).attr('name'));
})

回答by Vikram Deshmukh

It's pretty simple. Here's the demo

这很简单。这是演示

 <a name="temp"       onClick="updateArea1(this.name);">label</a>


 function updateArea1(el){
   alert( el)
 }

回答by Anirudhan J

When you assign a function to onclick event, you can access the element that triggered the event using this (If the function is global or in window scope).

当您将函数分配给 onclick 事件时,您可以使用 this 访问触发事件的元素(如果该函数是全局的或在窗口范围内)。

So just use this.namewithin updateArea1 function.

所以只需this.name在 updateArea1 函数中使用。

function updateAreaa1(){
  updateDb(this.name);  //this will refer to the element that triggered the click event.
}

Hope this helps.

希望这可以帮助。