jQuery+AJAX+PHP+MySQL数据库开辟搜刮功用,无跳转无革新搜刮。

知识点:ajax提交表单,php查询数据库,php返回json数组,javascript遍历输出json数组

演示:

1、当表单无输入任何关键词的时刻,返回“请输入关键词…”

《jQuery+AJAX+PHP+MySQL数据库开辟搜刮功用,无跳转无革新搜刮。》

2、当表单输入的关键词查询无果的时刻,返回“无效果”

《jQuery+AJAX+PHP+MySQL数据库开辟搜刮功用,无跳转无革新搜刮。》

3、当表单输入的关键词查询有效果,则返回效果。

《jQuery+AJAX+PHP+MySQL数据库开辟搜刮功用,无跳转无革新搜刮。》

表单页面

index.html

<!DOCTYPE html>
<html>
<head>    
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>AJAX搜刮</title>
    <style type="text/css">
        *{margin:0px;padding:0px;}
        h2{
            text-align: center;
        }

        #search_con{
            width: 300px;
            margin:10px auto;
        }

        #keywords{
            width: 300px;
            margin-top: 10px;
            height: 30px;
        }

        #btn{
            width: 305px;
            height: 35px;
            margin-top: 10px;
        }

        #search_result{
            width: 300px;
            margin:30px auto;
        }
    </style>
    <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        $(function(){
            $("button").click(function(){
                var inputVal = $("#keywords").val();
                $.ajax({
                    type:"GET",
                    url:"search.php?keywords=" + inputVal,
                    dataType:"json",
                    success:function(data){
                        $(function(){
                            var con="";
                            $.each(data,function(i,data){
                                if (data.result == "0") {
                                    con+="<p>请输入关键词...</p>"
                                }else if(data.result == "1"){
                                    con+="<p>无效果</p>"
                                }else{
                                    con+="<p>"+data.title+"</p>"
                                }
                            });
                                console.log(con);
                                $("#search_result").html(con);
                        })
                        return false; 
                    }
                })
            })
        })
    </script>
</head>
<body>
<!-- 表单 -->
    <div id="search_con">
    <form action="##">
        <h2>AJAX+PHP+MySQL搜刮</h2>
        <input type="text" name="keywords" id="keywords" placeholder="搜刮关键词..."><br/>
        <button name="button" type="button" id="btn">搜刮</button>
    </form>
    </div>
    <!-- 搜刮效果显现地区 -->
    <div id="search_result"></div>
</body>
</html>

服务端

search.php

<?php
header("Content-type:application/json");
//定义参数
$keywords = $_GET["keywords"];
//猎取数据库设置
require_once("config.php");
//衔接数据库
$con = mysql_connect($host,$username,$password);
  if (!$con)
    {
      die('衔接数据库失利,失利缘由:' . mysql_error());
    }
  //设置数据库字符集  
  mysql_query("SET NAMES UTF8");
  //查询数据库
  mysql_select_db($db, $con);
  //过滤关键词摆布空格
  $keyword = trim($keywords);
  if (empty($keyword)) {
  //假如关键词为空,则返回result=0
  echo "[{\"result\":\"0\"}]";
    }else{
      $result = mysql_query("SELECT * FROM $tb WHERE title like '%$keyword%' ORDER BY ID DESC");
      $num = mysql_num_rows($result);
      if ($num) {
        $search_result = array();
          while($row = mysql_fetch_array($result)){
              $search_result[] = $row;
          }
          // 将数组转成json花样
          echo json_encode($search_result);
  
  }else{
    //假如查询无果,则返回result=1
    echo "[{\"result\":\"1\"}]";
  }
}
?>

数据库设置

config.php

<?php
//设置文件 - BY TANKING
$host="localhost";
$username="root";
$password="root";
$db="test";
$tb="datalist";
?>

数据库构造

《jQuery+AJAX+PHP+MySQL数据库开辟搜刮功用,无跳转无革新搜刮。》

数据库名:test
表名:datalist
字段:id,title,url
字段剖析:
id – 自增ID
title – 题目
url – 页面链接

作者:TANKING
2018-7-12

    原文作者:TANKING
    原文地址: https://segmentfault.com/a/1190000015619516
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞