0

Background: I am attempting to run some AJAX on keyup in a search box that will go to the database for a lookup, then return the results without refreshing the page.

The Problem: I'm not confident that it's actually connecting to my database. I've tested the connection using THIS METHOD, and it says that it's successful for the credentials I'm using. However, I can change the host from locahost to www.MYDOMAINNAME.com OR the server name from my cPanel, and it still says it's a successful connection. If it's successful, then why isn't it running my SQL?

The Question: Is there something wrong with my code below, and if not, is there a better way for me to test what's happening?

Notes: The output in my console is "Error [object Object]". It's hitting search.php successfully, so I don't think it's a file path issue. I also ran the PHP on page load instead of doing it through AJAX and everything seemed to work just fine. I was able to get some results back when I hard-coded a value for $query.


File Structure:

(ROOT FOLDER)
   index.php (where the form is)
   (PHP FOLDER)
     search.php
   (JS FOLDER)
     search.js

HTML:

<form action="php/search.php" method="post">
    <input type="search" class="main-search" name="query" />
    <div class="btn btn-lg btn-primary">Search</div></form>
</form>

jQuery:

$(function() {
    $(".main-search").keyup(function() {
        search($(this).val());
    }); 
});

function search(query) {
    $.ajax({
        url:"./php/search.php",
        type:"POST",
        dataType:"json",
        data: { query:query },
        success: function(data) {
            $(".result").html(data);
        }, 
        error: function(data) {
            $(".result").html(data);    
        }
    });     
}

PHP:

<?php
    $query = $_POST["query"];

    $link = mysqli_connect('localhost', 'USER', 'PASS') or die ('Error connecting to mysql: ' . mysqli_error($link));

    mysqli_select_db($link, 'DB_NAME');

    /* check connection */
    if (mysqli_connect_errno()) {
        echo mysqli_connect_error();
        exit();
    }

    /* Select queries return a resultset */
    if ($result = mysqli_query($link, "SELECT actual FROM icons WHERE synonym = '$query'")) {
        $row = mysqli_fetch_array($result);
        echo $row["actual"];

        mysqli_free_result($result);
    }
    else {
        echo "No results found.";   
    }

    mysqli_close($con);
?>
Community
  • 1
  • 1
Jon
  • 3,154
  • 13
  • 53
  • 96
  • Press F12 IN Chrome and make sure the ajax call is made corectly or not. Check if it returns something. – Tejas Oct 30 '13 at 06:05
  • In the network tab of the Developer Tools, it's returning Error: [object Object]. – Jon Oct 30 '13 at 06:09
  • Could you please post the complete error part – Tejas Oct 30 '13 at 06:12
  • Notice: Undefined index: query in /home/jm/public_html/search/php/search.php on line 6. EDIT: I made this error go away by adding an isset() on $query. But that still doesn't fix my problem. – Jon Oct 30 '13 at 06:16

2 Answers2

0

Just navigate and run the php code directly instead of relying on an AJAX call to test. If there is a connection error then it should be displayed in your echo statement in the browser.

Make sure you have php error reporting running.

Resorath
  • 1,602
  • 1
  • 12
  • 31
0

Try changing the AJAX call data paramenter to this

data: { 'query':query }

I think this should work

Tejas
  • 585
  • 3
  • 15
  • So I'm almost there...it's performing the search now. But not on keyup. It doesn't seem to be passing the searchbox value to the PHP script. Why not? – Jon Oct 30 '13 at 06:28
  • Try alert in the keyup() to chk if its getting called. – Tejas Oct 30 '13 at 06:31