PHP/MySQL 在按钮按下/单击时运行查询 [英] PHP/MySQL run query on button press/click

查看:65
本文介绍了PHP/MySQL 在按钮按下/单击时运行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否可以在按下/点击按钮/链接时运行/重新运行 mysql 查询?

Was wondering if it's possible to run/re-run a mysql query when a button/link is pressed/clicked?

基本上,我运行的查询将结果限制为前 7 个,在结果下方有一个按钮以显示更多结果.单击/按下按钮时,我需要重新运行查询并将限制设置为 20.

Basically, I run a query that limits the results to the first 7, with a button below the results to show more results. When the button is clicked/pressed, I need to re-run the query and set the limit to, say 20.

任何有关如何执行此操作的帮助/建议将不胜感激.提前致谢,S.

Any help/advice on how to do this would be greatly appreciated. Thanks in advance, S.

推荐答案

运行查询并生成结果的代码在服务器上,按钮在浏览器上,因此您需要某种方式与服务器通信你想要更多的结果.您可以使用 Ajax 调用并动态更新您的页面内容,也可以将其设为链接并获取另一个页面,并获取更多结果.

The code to run the query and produce the results is on the server and the button is on the browser so you need some way to communicate back to the server that you want more results. You can either use an Ajax call and update your page's contents dynamically or just make it a link and GET another page with more results on.

您已经拥有返回 7 个结果并将它们显示在一个页面上的代码 - 假设您显示 7 个结果的页面是 www.blah.com/show.您可以在页面中添加一个参数来接受要显示的结果数量,例如www.blah.com/show?num=7.现在不是硬编码 SQL 来读取七个结果,而是首先读取参数并在查询中使用该数字.显示更多结果的链接现在可以是 <a href="www.blah.com/show?num=20">Show more</a>

You already have code that returns 7 results and show them on a page - let's say your page to show the seven is www.blah.com/show. You could add a parameter to your page that accepts the number of results to show, e.g. www.blah.com/show?num=7. Now instead of hardcoding the SQL to read seven results, you first read the parameter and use that number in your query. The link to show more results can now be <a href="www.blah.com/show?num=20">Show more</a>

Ajax 选项可能类似,但不是使用 <a> 从服务器检索新页面,而是使用(在 jQuery 中)$.ajax() 调用它,并且将结果插入到您的页面中.这比第一个例子稍微复杂一些.

The Ajax option could be similar, but instead of using a <a> to retrieve the new page from the server you would call it using (in jQuery) $.ajax() and would insert the results into your page. This is slightly more complicated than the first example.

这篇关于PHP/MySQL 在按钮按下/单击时运行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆