如何在ajax中搜索名称并从数据库中获取结果

时间:2017-09-09 05:48:04

标签: php jquery mysql ajax

这是我搜索名称的index.php页面。目前,我正在警惕电影的名称和年份。我在query.php页面中静态地给出了电影名称。 但我想输入电影名称在数据库中搜索并从query.php获取结果并在我的索引页面中显示结果,这样我就不必刷新页面..

///更新了我的代码但警告没有获取任何数据

<?php

include 'dbcon.php';

?>


<!DOCTYPE html>
<html>
<head>
    <title>ajax</title>

</head>
<body>
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script type="text/javascript">

function loaddata()

{

  var name = $('#moviename').val();

  $.ajax({
    type: "GET",
    url: "query.php",
    data: {
        name:name
    },
    success: function (data) {
        alert(data);

    }

    });



}

</script>

<p>Enter movie name </p>
<form action="" method="POST">
<input type="text" name="moviename" id="moviename">
<input type="submit" name="submit" value="Search" onclick="loaddata()">
</form>
<br>
<p>details here</p>
<div id="result">

</div>

</body>
</html>

这是query.php

<?php

    if(isset($_POST['submit']))

{


$name = $_POST['name'];

// $name ="rango";

$query = mysqli_query( $conn,"SELECT * FROM movie WHERE name = '$name'");

while($row = mysqli_fetch_array($query))
{
 echo "<p>".$row['name']."</p>";
 echo "<p>".$row['year']."</p>";
}



}


?>

4 个答案:

答案 0 :(得分:2)

请参阅下面的代码,我还包括对每次更新的评论。

here是点击事件的参考。 Sql Like语句reference

的index.php 你不需要包含dbcon.php b / c你没有在这里使用它

<!DOCTYPE html>
<html>
    <head>
        <title>ajax</title>
        <script
            src="https://code.jquery.com/jquery-2.2.4.min.js"
            integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
            crossorigin="anonymous"></script>
        <script type="text/javascript">

            // I moved the Javascript code to head section

            $(function() // this function excited if the jquery is ready i mean after jquery successfully loaded
            {
                function loaddata()
                {

                    var moviename= $("#moviename").val(); // read moviename value and assign;

                    $.ajax({
                        type: "GET",
                        url: "query.php",
                        data: {
                            name:moviename // there is no variable name so you have to assign the moveiname to name vairable ;
                        },
                        success: function (data) {
                           $("#result").html(data);

                        }

                    });

                }


                $("#submit").click(function(event) // Click Event Listener.
                {
                    event.preventDefault();
                    loaddata()
                });
            });



        </script>
    </head>
    <body>


        <p>Enter movie name </p>
        <form action="" method="POST">
            <input type="text" name="moviename" id="moviename">
            <input type="submit" name="submit" id="submit" value="Search"/>
            <!-- if you want ot use jquery you have to use event listener. like $("#submit").click(function(event){}); code from line 31 to 35 -->
        </form>
        <br>
        <p>details here</p>
        <div id="result">

        </div>

    </body>
</html>

query.php文件:

<?php

include 'dbcon.php';

$name =isset($_GET['name'])?$_GET['name']:''; // Change the static value to the dynamic value you sent from ajax request

// Use Like statement to filter name
$query = mysqli_query( $conn,"SELECT * FROM movie WHERE name like '%$name%'");

while($row = mysqli_fetch_array($query))
{
    echo "<p>".$row['name']."</p>";
    echo "<p>".$row['year']."</p>";
}

答案 1 :(得分:0)

使用类似查询

//for checkbox btn change
$('.fa-thumbs-o-up').on('click', function() {
  var LikeC = $('.imgtext').last();
  var LikeA = $('.imgtext').first();
  if (!LikeC.hasClass("hidechktext")) {
    LikeA.removeClass('hidechktext');
  }
  $(this).siblings().parents('.imgtext').next().removeClass('hidechktext');
  $(this).parents('.imgtext').addClass('hidechktext');
});

//for image change    

$('.fa-thumbs-o-up').on('click', function() {
  $('.imgall').children().siblings('.imgmain:first').removeClass('hidechktext'); 
  $('.imgmain').prepend().addClass('hidechktext');
});

并使用post方法

$query = mysqli_query( $conn,"SELECT * FROM movie WHERE name like '%$name%'");

并在您的query.php中将名称检索为

 $.ajax({
type: "POST",
url: "query.php",
data: {
    name:name
},
success: function (data) {
    alert(data);

}

});

答案 2 :(得分:0)

您没有将电影名称解析为<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:org:jgroups" xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/jgroups.xsd"> <TCP bind_port="7800" recv_buf_size="${tcp.recv_buf_size:130k}" send_buf_size="${tcp.send_buf_size:130k}" max_bundle_size="64K" sock_conn_timeout="300" thread_pool.min_threads="0" thread_pool.max_threads="20" thread_pool.keep_alive_time="30000"/> <TCPPING async_discovery="true" initial_hosts="${jgroups.tcpping.initial_hosts:10.120.19.145[7800],localhost[7801]}" port_range="2" /> <MERGE3 min_interval="10000" max_interval="30000"/> <FD_SOCK/> <FD timeout="3000" max_tries="3" /> <VERIFY_SUSPECT timeout="1500" /> <BARRIER /> <pbcast.NAKACK2 use_mcast_xmit="false" discard_delivered_msgs="true"/> <UNICAST3 /> <pbcast.STABLE desired_avg_gossip="50000" max_bytes="4M"/> <pbcast.GMS print_local_addr="true" join_timeout="2000" view_bundling="true"/> <MFC max_credits="2M" min_threshold="0.4"/> <FRAG2 frag_size="60K" /> <pbcast.STATE_TRANSFER/> </config> 所以首先必须将电影名称传递给ajax.php 请更新ajax.php

中的js代码
index.php

function loaddata() { var name = $('#moviename').val();/* get movie_name in name variable*/ $.ajax({ type: "GET", url: "query.php", data: { name:name }, success: function (data) { alert(data); } }); }

ajax.php

答案 3 :(得分:0)

首先,将您的ajax调用类型从GET更改为POST 使用ajax将要发送的数据更改为此

data: {
        name1:name
    },

并在php中将其作为

$name = $_POST['name1'];

以及你在php中回应的内容 将它们全部附加到一个变量上,然后回显它...类似

    $query = mysqli_query( $conn,"SELECT * FROM movie WHERE name = '%$name%'");
    $response = "";
    while($row = mysqli_fetch_array($query))
    {
      $response = $response."<p>".$row['name']."</p><p>".$row['year']."</p>";
    }
      echo $response;