下面是我想在http://geheimprojekt.nomachines.org/上完成的任务
这是我第一次尝试使用AJAX。我有jQuery的知识,但我似乎不能把这些点联系起来。
SQL
CREATE TABLE IF NOT EXISTS `sggcount` (
`counter` bigint(20) NOT NULL DEFAULT '2'
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci;
--
-- Dumping data for table `sggcount`
--
INSERT INTO `sggcount` (`counter`) VALUES
(2);发布于 2012-04-20 12:04:57
要让它发挥作用是非常简单的。您需要为将来的div提供一些html,您想在其中放置童子军:
<div id="counting"></counting>然后,在生成器()函数的末尾添加以下内容:
function generator(){
/*your code here...*/
var element = document.createElement("div");
element.setAttribute("id", "result");
element.appendChild(document.createTextNode(name));
document.getElementById("placeholder").appendChild(element);
/*the ajax code here*/
var url='urltophpfile/phpfile.php';
$.get(url,function(data){
$('#counting').html(data+' Word combinations have been generated so far.');
});
}现在,在您的phpfile.php文件中,您将需要代码来增加计数。如果现在我也能帮上忙的话,我想你知道该怎么做了。我将在这里添加一些示例代码,这样您就有了一个想法。
<?php
mysql_connect('localhost', 'db-sgg', 'password') or die('Cannot connect to database server');
mysql_select_db('db1152127-sgg') or die('Cannot select database');
$databasecall = mysql_query("SELECT counter FROM sggcount WHERE counter > 1");
/*so here you select the already stored value and then you make an update to increment it*/
mysql_query("UPDATE sggcount SET counter=counter+1");
$count = mysql_fetch_assoc($databasecall);
echo $count['counter']+1;
?>通过执行上面的回显操作,您将返回递增的值,ajax将显示它。
更新1
添加了更全面的php代码
注意:如果添加jquery脚本,请将生成器函数更改为使用jquery。
发布于 2012-04-20 11:58:47
使用jQuery,您可以将单击事件绑定到按钮并发出ajax请求。
在服务器端,PHP页面应该更新SQL数据。遵循Javascript演示代码
$(document).ready(function(){
$('button-selector').click(function(){
//use jquery ajax call to call php server page that update SQL data
$.ajax({
url: "updateClick.php",
context: document.body
}).success(function() {
//success callback
});
});
});发布于 2012-04-20 12:00:21
若要发送AJAX请求,请单击“使用”:
$('#button').click(function(){ // when user `click` element with `id="button"` (#button)
$.ajax({ // Start AJAX call
url: 'accept.php', // URL to send AJAX request
success: function(data) { // Function to execute on SUCCESS reply (reply data as paramenter)
var cc = $('#clicks_count').html(); // In your element with `id="clicks_count"` you store your click count (`<a id="clicks_count">21</a>`). Assign to `cc` javascript variable value of clicls_count
$('#clicks_count').html(cc + 1); // Increasing clicls_count on 1 and write it to `<a id="clicks_count">22</a>`
}
});
});在accept.php使用脚本时,点击次数增加1。
https://stackoverflow.com/questions/10245693
复制相似问题