PHP搜索函数不显示结果

时间:2022-09-10 19:13:57

I've been playing around with PHP and ajax and trying to get it to produce my results. For some reason though, it's not working at all. I will write in some words and nothing will appear.

我一直在玩PHP和ajax,试图让它产生我的结果。但出于某种原因,它根本就不管用。我会写一些字,什么也不会出现。

Code:

代码:

    <script type="text/javascript">
        $( document ).ready( function() {
            $('.searchFunction').keyup( function( event ) {
                var search_term = $(this).attr('value');

        });
    </script>

Again, I am not really sure how to fix this. My database is called ing and the name of the field is ingName.

再说一遍,我也不知道该怎么解决这个问题。我的数据库名为ing,字段名是ingName。

3 个解决方案

#1


2  

Got it: you were not getting text box value here var search_term = $(this).val();

得到:这里没有得到文本框值var search_term = $(this).val();

The OP was trying to read the value of the text box using var search_term = $(this).attr('value');

OP尝试使用var search_term = $(this).attr('value')读取文本框的值;

There was issue in your select query, I have updated it.

你的select查询有问题,我已经更新了。

Update 1:

更新1:

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' )
    {
        $conn=mysql_connect("localhost","root","") or die ("could not connect");
        mysql_select_db("test") or die ("could not find db");

        if ( !empty( $_POST['search_term'] ) ) 
        {

            $search_term =  $_POST['search_term'] ;

           $query = mysql_query( "SELECT `ingName` FROM `ing` WHERE `ingName` LIKE '".$search_term.'%', $conn );


            if( $query )
            {
                while( $row = mysql_fetch_assoc( $query ) ) 
                {
                    echo '<li>'.$row['ingName'].'</li>';
                }
            }
        }
        mysql_close( $conn );
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dashboard</title>
  </head>
  <body>

    <div class="container">
        <input type="text" name='search_term' class="searchFunction">
        <input type="submit" value="Search">
        <div class="dropdown">
            <ul class="result"></ul>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $( document ).ready( function() {
            $('.searchFunction').keyup( function( event ) {
                var search_term = $(this).val();

                $.post( document.location.href, { search_term:search_term }, function( data ) {
                    $('.result').html( data );

                    $('.result li').click( function( event ) {
                        var result_value = $(this).text();

                        $('.searchFunction').attr('value', result_value );
                        $('.result').html('');
                    });
                });
            });
        });
    </script>

  </body>

#2


1  

Run your query in the MySQL command line, and try to get results. Do you get a response?

在MySQL命令行中运行查询,并尝试获得结果。你得到回应了吗?

Next point: you use wrong concatenation. I mean, change this

下一点:使用错误的连接。我的意思是,改变这一现状

,$row['ingName'],

to this

这个

. $row['ingName'] .

#3


0  

Select your textbox by id or class. Here I am doing it using id, getting the value of the textbox when a key is pressed, and passing the entered value in ajax code.

按id或类选择文本框。在这里,我使用id进行操作,在按下键时获取文本框的值,并在ajax代码中传递输入值。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' )
    {
        $conn=mysql_connect("localhost","root","") or die ("could not connect");
        mysql_select_db("test") or die ("could not find db");

        if ( !empty( $_POST['search_term'] ) ) 
        {

            $search_term = mysql_real_escape_string( $_POST['search_term'] );

            $query = mysql_query( "SELECT `ingName` FROM `ing` WHERE `ingName` LIKE '$search_term%'", $conn );


            if( $query )
            {
                while( $row = mysql_fetch_assoc( $query ) ) 
                {
                    echo '<li>'.$row['ingName'].'</li>';
                }
            }
        }
        mysql_close( $conn );
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dashboard</title>
  </head>
  <body>

    <div class="container">
        <input type="text" name='search_term' id="search_term" class="searchFunction">
        <input type="submit" value="Search">
        <div class="dropdown">
            <ul class="result"></ul>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $( document ).ready( function() 
        {
            $('.searchFunction').keyup( function( event ) 
            {
                var search_term = $("#search_term").val();

                $.post( document.location.href, { search_term:search_term }, function( data ) 
                {
                    $('.result').html( data );

                    $('.result li').click( function( event ) 
                    {
                        var result_value = $(this).text();
                        $('.searchFunction').attr('value', result_value );
                        $('.result').html('');
                    });
                });
            });
        });
    </script>
  </body>
</html>

#1


2  

Got it: you were not getting text box value here var search_term = $(this).val();

得到:这里没有得到文本框值var search_term = $(this).val();

The OP was trying to read the value of the text box using var search_term = $(this).attr('value');

OP尝试使用var search_term = $(this).attr('value')读取文本框的值;

There was issue in your select query, I have updated it.

你的select查询有问题,我已经更新了。

Update 1:

更新1:

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' )
    {
        $conn=mysql_connect("localhost","root","") or die ("could not connect");
        mysql_select_db("test") or die ("could not find db");

        if ( !empty( $_POST['search_term'] ) ) 
        {

            $search_term =  $_POST['search_term'] ;

           $query = mysql_query( "SELECT `ingName` FROM `ing` WHERE `ingName` LIKE '".$search_term.'%', $conn );


            if( $query )
            {
                while( $row = mysql_fetch_assoc( $query ) ) 
                {
                    echo '<li>'.$row['ingName'].'</li>';
                }
            }
        }
        mysql_close( $conn );
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dashboard</title>
  </head>
  <body>

    <div class="container">
        <input type="text" name='search_term' class="searchFunction">
        <input type="submit" value="Search">
        <div class="dropdown">
            <ul class="result"></ul>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $( document ).ready( function() {
            $('.searchFunction').keyup( function( event ) {
                var search_term = $(this).val();

                $.post( document.location.href, { search_term:search_term }, function( data ) {
                    $('.result').html( data );

                    $('.result li').click( function( event ) {
                        var result_value = $(this).text();

                        $('.searchFunction').attr('value', result_value );
                        $('.result').html('');
                    });
                });
            });
        });
    </script>

  </body>

#2


1  

Run your query in the MySQL command line, and try to get results. Do you get a response?

在MySQL命令行中运行查询,并尝试获得结果。你得到回应了吗?

Next point: you use wrong concatenation. I mean, change this

下一点:使用错误的连接。我的意思是,改变这一现状

,$row['ingName'],

to this

这个

. $row['ingName'] .

#3


0  

Select your textbox by id or class. Here I am doing it using id, getting the value of the textbox when a key is pressed, and passing the entered value in ajax code.

按id或类选择文本框。在这里,我使用id进行操作,在按下键时获取文本框的值,并在ajax代码中传递输入值。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' )
    {
        $conn=mysql_connect("localhost","root","") or die ("could not connect");
        mysql_select_db("test") or die ("could not find db");

        if ( !empty( $_POST['search_term'] ) ) 
        {

            $search_term = mysql_real_escape_string( $_POST['search_term'] );

            $query = mysql_query( "SELECT `ingName` FROM `ing` WHERE `ingName` LIKE '$search_term%'", $conn );


            if( $query )
            {
                while( $row = mysql_fetch_assoc( $query ) ) 
                {
                    echo '<li>'.$row['ingName'].'</li>';
                }
            }
        }
        mysql_close( $conn );
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dashboard</title>
  </head>
  <body>

    <div class="container">
        <input type="text" name='search_term' id="search_term" class="searchFunction">
        <input type="submit" value="Search">
        <div class="dropdown">
            <ul class="result"></ul>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $( document ).ready( function() 
        {
            $('.searchFunction').keyup( function( event ) 
            {
                var search_term = $("#search_term").val();

                $.post( document.location.href, { search_term:search_term }, function( data ) 
                {
                    $('.result').html( data );

                    $('.result li').click( function( event ) 
                    {
                        var result_value = $(this).text();
                        $('.searchFunction').attr('value', result_value );
                        $('.result').html('');
                    });
                });
            });
        });
    </script>
  </body>
</html>