需要 Ajax 函数 - 在不刷新页面的情况下更新数据库
Need Ajax function - update db without refresh of page
我需要帮助一个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.
}
});
相关文章:
- 如何在不刷新页面的情况下更新显示框
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 在不重定向页面的情况下更新表
- Angular/UI路由器-如何在不刷新所有内容的情况下更新URL
- 如何在不按ctrl+F5的情况下更新我的Web应用程序(chrome、mozilla、IE8+)更改
- 可以在不重新加载页面的情况下更新javascript中的cookie
- 在不重新加载控制器的情况下更新路由
- 在不重新加载页面的情况下更新 URL 哈希
- 在不重新加载页面的情况下更新块中的内容
- 需要 Ajax 函数 - 在不刷新页面的情况下更新数据库
- 在不加载页面的情况下更新谷歌地图上的标记位置
- 在不刷新的情况下更新标签内的变量
- 在不超出页面的情况下更新数据库
- 如何在不使用映射的情况下更新视图模型
- 在不刷新页面的情况下更新 SQL 数据
- 如何在代码点火器中使用 websocket 在不刷新页面的情况下更新内容
- 如何在不刷新浏览器的情况下更新倒计时达到 0 时的日期
- 在不触发状态更改的情况下更新 URL
- 在不重新加载整个页面的情况下更新网页的特定区域
- GULP:如何在不刷新的情况下更新浏览器(仅适用于 CSS 更改)