问题描述
下面是我想要完成的 http://geheimprojekt.nomachines.org/
- 在用户点击Nochmal!按钮(产生新的组合词)
- 将点击我的MySQL数据库(withou重新加载页面),增加点击行1
- 更新段落中的文字N字组合已经产生了这么远。
这是我与AJAX工作的第一次尝试。我有jQuery的知识,但我无法连接似乎点。
在SQL
CREATE TABLE IF NOT EXISTS`sggcount`(
`counter` BIGINT(20)NOT NULL DEFAULT'2'
)ENGINE = MyISAM数据默认字符集=拉丁文COLLATE = latin1_german2_ci;
-
- 倾销数据表`sggcount`
-
INSERT INTO`sggcount`(`counter`)VALUES
(2);
要得到这个工作很简单。您需要在要放置couting一些HTML未来的DIV:
< DIV ID =计数>< /计数>
然后就在发电机的end()函数你补充一点:
函数发生器(){
/ *您code在这里... * /
VAR元= document.createElement方法(分区);
element.setAttribute(ID,结果);
element.appendChild(document.createTextNode(名称));
的document.getElementById(占位符)的appendChild(元)。
/ *阿贾克斯code这里* /
VAR URL ='urltophpfile / phpfile.php;
$获得(URL,功能(数据){
$('#计数)HTML(数据+'字的组合已经产生了这么远。);
});
}
现在你phpfile.php文件,您需要在code递增计数。我想你知道如何做到这一点的一部分,如果现在我能帮助它。我将添加一些样品code在这里,所以你有一个想法。
< PHP
的mysql_connect(本地主机,DB-SGG,密码)或死亡(无法连接到数据库服务器);
mysql_select_db('db1152127 - SGG)或死亡(无法选择数据库);
$ databasecall =请求mysql_query(选择对抗来自sggcount WHERE柜台→1);
/ *所以在这里选择已存储的值,然后你做一个更新来增加它* /
请求mysql_query(更新sggcount SET计数器=计数器+ 1);
$数= mysql_fetch_assoc($ databasecall);
回声$计数['反'] + 1;
?>
这样做回声上面,你将返回递增值和阿贾克斯将显示它。
更新1
增加了更多的COM prehensive PHP code
请注意:如果您添加jQuery脚本,请改变发电机的功能使用jQuery
Here is what I want to accomplish on http://geheimprojekt.nomachines.org/
- User clicks on "Nochmal!" Button (New word combination is generated)
- Send the click to my MySQL database (withou reloading the page), increase "clicked" row by 1
- Update the text in a paragraph "n Word combinations have been generated so far."
This is my first attempt to work with AJAX.I have jQuery knowledge but i can't connect the dots it seems.
The 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);
To get this to work is very simple. You need some html for the future div where you want to place the couting:
<div id="counting"></counting>
Then right at the end of the generator() function you add this:
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.');
});
}
Now in your phpfile.php file you will need the code to increment the count. I guess you know how to do this part if now i can help with it too. I'll add some sample code here so you have an idea.
<?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;
?>
By doing the echo above you will return the incremented value and the ajax will display it.
Update 1
Added more comprehensive php code
NOTE: if you add the jquery script please change the generator function to use jquery.
这篇关于指望一个按钮点击,将其保存到MySQL,然后显示当前值(AJAX,PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!