大佬教程收集整理的这篇文章主要介绍了结合JQuery / PHP将点击记录到数据库中?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_7@我希望每次用户点击结果时进行记录(出于学习/智能目的)并将其存储在我创建的数据库表中,该表存储会话ID,查询,结果的位置以及结果的顺序.用户点击了该项目.
@H_502_7@使用JQuery,我已经有了一个函数,它将拉出被点击的结果的标题,我将它设置在我想要记录点击的位置,但我不知道如何做到这一点,因为JQuery是客户端和PHP是服务器端.@H_502_7@如何使用JQuery触发PHP函数,以便我可以查询数据库以将单击日志插入到我的表中?
@H_502_7@下面是JQuery函数.@H_502_7@
$(document).ready(function() { $('.accordionButton').click(function(E) { if($(this).next().is(':hidden') == truE) { $(this).addClass('on'); $(this).next().slideDown('normal'); $(this).next().slideDown(test_accordion); // SEND CLICK ACTION TO LOG INTO THE DATABASE alert($(this).find('h3:last').text()); // displays the title of the result that was just clicked } else { $(this).removeClass('on'); $(this).next().slideUp('normal'); $(this).next().slideUp(test_accordion); } }); }
@H_502_7@定义一个javascript变量来跟踪点击功能之外的点击顺序:
@H_502_7@
var order = 0;@H_502_7@将其添加到您的点击功能中,位于底部: @H_502_7@
order++; var sessionID = $("input[name='sessionID']").val(); // assuming you have sessionID as the value of a hidden input var query = $("#query").text(); // if 'query' is the id of your searchBox var pos = $(this).index() + 1; // might have to modify this to get correct index $.post("logClick.PHP",{sessionID:sessionID,query:query,pos:pos,order:order});@H_502_7@在你的PHP脚本名为“logClick.PHP”(在同一目录中): @H_502_7@
<?PHP // GET AJAX POSTED DATA $str_sessionID = empty($_POST["sessionID"]) ? '' ; $_POST["sessionID"]; $str_query = empty($_POST["query"]) ? '' ; $_POST["query"]; $int_pos = empty($_POST["pos"]) ? 1 ; (int)$_POST["pos"]; $int_order = empty($_POST["order"]) ? 1 ; (int)$_POST["order"]; // CONNECT TO DATABASE if ($str_sessionID && $str_query) { require_once "dbconnect.PHP"; // include the commands used to connect to your database. Should define a variable $con as the MysqL connection // INSERT INTO MysqL DATABASE TABLE CALLED 'click_logs' $sql_query = "INSERT INTO click_logs (sessionID,query,pos,order) VALUES ('$str_sessionID','$str_query',$int_pos,$int_order)"; $res = MysqL_query($sql_query,$con); if (!$res) die('Could not connect: ' . MysqL_error()); else echo "Click was logged."; } else echo "No data found to log!"; ?>@H_502_7@如果要查看脚本中是否发生错误,可以添加回调函数作为$.post()ajax方法的第三个参数: @H_502_7@
$.post("logClick.PHP",order:order},function(result) { $('#result').html(result); // display script output into a div with id='result' // or just alert(result); }) );@H_502_7@编辑:如果您需要在页面加载之间保持order变量的值,因为您对结果进行了分页,那么您可以使用GET或POST在页面之间传递此变量的值.然后,您可以将值保存在隐藏的输入中,并使用jQuery轻松读取它. (或者你也可以使用COokies). @H_502_7@示例(将其放在每个结果页面中):
@H_502_7@
<?PHP $order = empty($_POST["order"]) ? $_POST["order"] : "0"; $html="<form id='form_session' action='' name='form_session' method='POST'> <input type='hidden' name='order' value='$order'> </form>\n"; echo $html; ?>@H_502_7@在你的jQuery中,只需更改var order = 0;至
@H_502_7@
var order = $("input[name='order']").val();@H_502_7@然后,当用户单击页面链接时,阻止默认链接操作,设置订单值和表单操作,然后使用javascript / jQuery提交表单:
@H_502_7@
$("a.next_page").click(function(event) { event.preventDefault(); var url = $(this).attr("href"); $("input[name='order']").val(order); $("#form_session").attr('action',url).submit(); });@H_502_7@所有“下一个”和“前一个”分页链接必须被赋予相同的类(即“next_page”(在此示例中). @H_502_7@
<div class='pagination'> <ul><li><a href='page1.url'>1</a></li> <li><a href='page2.url'>2</a></li> </ul> </div>@H_502_7@然后改变这个: @H_502_7@
$("div.pagination a").click(function(event) { etc.
以上是大佬教程为你收集整理的结合JQuery / PHP将点击记录到数据库中?全部内容,希望文章能够帮你解决结合JQuery / PHP将点击记录到数据库中?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。