通过使用JavaScript的onClick数据数据库 [英] Pass data to database using javascript Onclick
问题描述
我是一个真正的noob当涉及到的JavaScript / AJAX,所以任何帮助将是非常美联社preciated。 在谈到这个问题:
I am a real noob when it comes to javascript/ajax, so any help will be very appreciated. In reference to this question:
<一个href="http://stackoverflow.com/questions/638147/updating-a-mysql-database-using-php-via-an-onclick-javascript-function">Updating通过一个onClick javascript函数使用PHP MySQL数据库
Updating a MySql database using PHP via an onClick javascript function
但主要关注的是由菲尔圣的离开了答案。我想知道,如果有人能阐述一下我们是如何(如果我们可以吗?)通过他的例子传递值/数据,使用jQuery。 他所留下的code示例如下:
But mainly concerned with the answer left by Phill Sacre. I am wondering if someone could elaborate on how we are(if we can?) passing values/data through his example, using jquery. The code example left by him is as follows:
function updateScore(answer, correct) {
if (answer == correct) {
$.post('updatescore.php');
}
}
...
<a onclick="updateScore(this, correct)" ...> </a>
比方说,我们是想给任何数量的值传递给用PHP数据库,可能有人给我所需要的JavaScript函数的一个片段的例子吗?或者阐述什么上面贴吗?
Say for example, we are wanting to pass any number of values to the database with php, could someone give me a snippet example of what is required in the javascript function? Or elaborate on what is posted above please?
再次感谢所有。
推荐答案
我能想到的最简单的例子是这样的。让你的AJAX调用在如果这样的块:
The simplest example I can think of is this. Make your AJAX call in your if block like this:
$.get('updatescore.php', {'score': '222'}, function(d) {
alert('Hello from PHP: ' + d);
});
在你的updatescore.php的剧本,只要做到这一点:更新得分。而返回的明文的说明阉更新操作成功与否。
On your "updatescore.php" script, just do that: update the score. And return a plain text stating wether the update operation was successful or not.
祝你好运。
PS:你也可以使用发表而不是获取
P.S.: You could also use POST instead of GET.
这篇关于通过使用JavaScript的onClick数据数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!