1

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.

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.

4
  • nothing at all , Its weird Commented Feb 29, 2016 at 17:32
  • @Nevershow2016 your code is running. Check your console. I just verified it. Something wrong with your query. Commented Feb 29, 2016 at 17:37
  • @Nevershow2016 got the issue check my code below. Commented Feb 29, 2016 at 17:40
  • Try to catch error using mysql_error($con) after your query Commented Feb 29, 2016 at 17:48

3 Answers 3

2

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

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

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

Update 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>
Sign up to request clarification or add additional context in comments.

16 Comments

Can you please explain what changes you made here ?
You're trying to read an attribute in the element while instead you want the value var search_term = $(this).val();
tRIED this is well and still nothing
@Nevershow2016 check my updated code. Let me know if it works.
Howeer it did not add to the box again i am not sure why
|
1

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

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

,$row['ingName'],

to this

. $row['ingName'] .

3 Comments

This may be one of the reason. Good Catch
Were should this go>?
echo '<li>',$row['ingName'],'</li>'; to echo '<li>'.$row['ingName'].'</li>';
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.

<?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>

4 Comments

An Explanation To This Answer Will Be More Useful. What Changes You Did In Code & Why It Will Work. Don't Simply Write "Try This Example".
This is great it actually worked perfectly, but yes, what changes were made , jut so i know
@PareshGami there is only 1 thing wrong, when u click on it, it does not appear in the box
So it will become a drop down box and its there but when i click on it , it should show up in the search box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.