我需要帮助一个ajax函数
这是原始页面设置。页面将包含带有此表单链接的表格
....链接 - 按钮1 - 按钮2链接 - 按钮1 - 按钮2链接 - 按钮1 - 按钮2...
其中 button1 用于隐藏行,button2 用于隐藏 + 添加 + 1 值到数据库中的链接。每个链接都有与 Button2 一起使用的唯一 ID,因此很容易定位它。因此,访问者将单击连续中的一个按钮,行将隐藏,然后他移动到另一行。
数据库设置:
id - link - .... - nmbOFlikes
我的问题是我不了解 Ajax,这是更新数据库的唯一解决方案,无需在每次单击按钮后刷新。
该页面不是静态的,它由另一个从数据库中提取数据的函数格式化这是简单的html页面版本,所以如果有人可以提供帮助...
所以这是没有函数的javascript
$(document).ready(function(){
$("button.live").click(function(){
$(this).parents('tr').hide();
alert('Link is hidden');
});
$("button.add").click(function(){
$(this).parents('tr').hide();
alert('This link is dead');
$.post("update_db.php", { id: button_id, increment: true },
function(data) {
alert("Link incremented");
});
}); });
这是表
<table width="500" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><p>Link 1</p></td>
<td><button class="live">live</button></td>
<td><button class="add" id="1">add</button></td>
</tr>
<tr>
<td><p>Link 2</p></td>
<td><button class="live">live</button></td>
<td><button class="add" id="2">add</button></td>
</tr>
</table>
您不会将值直接添加到数据库中,而是先将数据发布到脚本。 我不完全清楚您要完成什么,但 post 函数可能如下所示:
$.post("update_database_with_ajax.php", { id: button_id, increment: true },
function(data) {
alert("Link incremented");
});
下面是一个函数式 jsFiddle 示例:Jquery POST 示例
update_data_With_ajax.php
/** This is an example and should not be used 'as is' **/
if ( isset( $_REQUEST['increment'] ) {
// Connect to MySQL
$conn = mysql_connect("localhost", "root", "");
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
// Fetch the values we posted using AJAX
$id = mysql_real_escape_string( $_REQUEST['id'] );
//Select your database
mysql_select_db("my_db", $conn);
//increment your number of clicks
mysql_query("UPDATE table_name SET nmbofclicks = nmbofclicks + 1 WHERE id = {$id}");
}
jQuery.ajax 的简单用法:
$.ajax({
url: "serversite.php",
type: "POST",
data: {
// Object of data you will send to serversite.php
},
success: function() {
// everything went ok.
}
});