我正在搜索代码拆分。我想链接首先点击运行MySQL查询并更改链接。
我发现了这一点,但只是改变文字:
<script type="text/javascript">
$(document).ready(function() {
// hides the slickbox as soon as the DOM is ready
// (a little sooner than page load)
$('#slickbox').hide();
// toggles the slickbox on clicking the noted link
$('a#slickbox-toggle').click(function() {
$('#slickbox').slideToggle(400);
return false;
});
});
</script>
$(this).text($(this).text() == 'Show box' ? 'Hide box' : 'Show box');
实施例: Facebook喜欢button =&gt;
赞(56赞)
不同
点击(更新喜欢...... ..... likecount + = 1) 与(57赞)
我该怎么做?
答案 0 :(得分:0)
您必须在onclick
处理程序中执行AJAX请求。这将需要与某种进行SQL查询的Web服务或脚本进行交互。
规范是使用AJAX向使用MySQL API与MySQL数据库交互的PHP脚本发送请求。
通过Google搜索这些关键字会为您提供许多可以遵循的示例和教程。
答案 1 :(得分:0)
所需文件:
代码:
<?php
$host=""; //your hostname
$username="";//database username
$database="";// database name
$password="":// database password
$connect=mysql_connect($host,$username,$password);
$select=mysql_select_db($database,$connect);
if(!$select)
exit("Wrong credentials in config file");
?>
session.php&lt; - 存储登录的用户会话
<?php
session_start();
$_SESSION['id'];
?>
ajaxlike.php&lt; - 通过ajax方法调用
代码:
<?php
require_once('session.php');
require_once('config.php');
$user=$_SESSION['id'];
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> </script>
<script type="text/javascript">
$(".likes").click(function(){
var c=$(this).attr("id");
$(".likes#"+c).html('');
$(".likes#"+c).html('<center><img src="http://www.blogkeen.com/images/loading_fb.gif" height="16"></center>');
$.ajax({
type: "POST",
url: "ajaxlike.php",
data: {id:c},
cache : false,
success: function(data) {
$(".likes#"+c).html('');
$(".likes#"+c).append(data);
}
});
});
</script>
<?php
$id=$_POST['id'];
if($id)
{
$check=mysql_query("SELECT * FROM likes WHERE liker='$user' AND liked='$id'");
$get=mysql_num_rows($check);
echo "<a class=\"likes\" id=\"l{$id}\" style=\"cursor:pointer;cursor:hand;\">";
if($get)
{
$del=mysql_query("DELETE FROM likes WHERE liker='$user' AND liked='$id'");
echo "Like";
}
else
{
$insert=mysql_query("INSERT INTO likes(liker,liked) VALUES('$user','$id')");
echo "Unlike";
}
echo "</a>";
}
?>
index.php&lt; - 从任何项目的位置放置
<?php
require_once('session.php');
require_once('config.php');
$id=$_SESSION['id'];
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(".likes").click(function(){
var c=$(this).attr("id");
$(".likes#"+c).html('');
$(".likes#"+c).html('<center><img src="http://www.blogkeen.com/images/loading_fb.gif" height="16"></center>');
$.ajax({
type: "POST",
url: "ajaxlike.php",
data: {id:c},
cache : false,
success: function(data) {
$(".likes#"+c).html('');
$(".likes#"+c).append(data);
}
});
});
</script>
<a id="1" class="likes">Like 1</a><br/>
<a id="2" class="likes">Like 2</a><br/>
<a id="3" class="likes">Like 3</a><br/>
数据库结构: 运行此查询以创建表
CREATE TABLE likes
(
id int primary key NOT NULL AUTO_INCREMENT,
liker int,
liked int
)