dousui7410 2014-01-08 15:02
浏览 32

使用ajax根据搜索显示结果?

So currently, I have a php page which displays a list of results which it gets from my sql database. It displays the ID and the names.

I have a search at the top which just searches for keywords in the "name" field then if search is clicked it will take you to a new page which shows the results you have searched for.

What I am trying to do is to make it Asynchronous so when I search for lets say "London" it displays only results which have the keyword London in it, but on the same page.

My search button code:

    <input type="button" class="search" value= "Search">

Ajax code which I have tried using to make it asynchronous:

 $(document).ready(function() {
 $(".search").each(function() {
 var btn = $(this);
 btn.on("click", function(){
 $.post("searchresults.php",
 function(data){
 .append(data);}
 }}})

I'm new to this so apologies if it's a silly mistake. My searchresults.php just has the SELECT * FROM Property WHERE etc.. and then it echos the results.

Thanks.

  • 写回答

2条回答 默认 最新

  • doxrxwn2252 2014-01-08 15:06
    关注

    You need to have some element on your page that'll hold the search results, e.g.

    <div id="result"></div>
    

    and then in your .post() call, you'd have a result handler that does

    function (results) {
        $('#result').text(results);
    }
    

    Exactly how you actually insert/display the data is up to you - your PHP script could send over a full-blown html snipper, or a nice JSON-encoded data structure and your JS builds up the results, etc... This is just a simple brain-dead "here's some result data and display it as-is".

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值