1

Alright so I have this code which basically finds the user inside the table users and displays it in alert, but it seems that I am doing something wrong. The log shows "Function is not set" and the alert itself displays that.

This is the HTML form I have for it

<center><form method='POST' >
    <input id="search_fix" type="text" name="search" placeholder="Search..">
    <input type="submit" name="submit_menu_search" style="display: none;">
</form></center>

This is the ajax processing

$(document).ready(function() {

    $("#search_fix").keyup(function() {
        var search_text = $(this).val();

        if(search_text != '') {
            $.ajax({  
                url:"handler.php",  
                method:"POST",  
                data:{"function":"search_ajax", search:search_text},  
                dataType:"text",  
                success:function(data){  
                    $('#search_result').html(data);
                    console.log(data);
                    alert(data);
                }  
            });  
        }
        else {

        }
    });

});

And these are my PHP functions that I used to basically search for the term

public function search_ajax($term) {
    $handler = new sql();
    $sql = $handler->connect();
    $sql->real_escape_string($term);

    $result = $sql->query("SELECT ime FROM users WHERE ime LIKE '%".$term."%'") or die (mysql_error());
    if($result->num_rows >= 1){ 
        while($row = $result->fetch_assoc()) {
            echo $row['ime'];
        }
    }

}
if(isset($_POST['function'])) {
    switch($_POST['function']) {
    case "search_ajax": {
        require_once "assembly/user.php";
        $user = new User();
        $user->search_ajax($_POST['search']);
        break;
    }


    default: {
        echo "Unknown AJAX function handler";
        break;
    }
    }
}
else {
    echo "Function is not set";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    You're assuming that the ajax request is going to be successful. So if it is not you won't know, it will look like nothing was returned. – Sam Orozco Oct 18 '16 at 19:00
  • Well if its not successful it should return echo "Function is not set"; correct? Since its returning that at the moment – Tomislav Nikolic Oct 18 '16 at 19:01
  • check browser console network tab.also check post data from server `print_r($_POST)` – Madhawa Priyashantha Oct 18 '16 at 19:02
  • 2
    Your format for data:{"function":"search_ajax", search:search_text}, is wrong. Refer [here](http://stackoverflow.com/questions/3548802/call-php-function-from-jquery) for more info – Rex Oct 18 '16 at 19:03
  • 1
    No that would be successful according to the javascript. Under your success case you want to add `, error: function (xhr, status, error) { alert(xhr.responseText); alert(status); alert(error); }` – Sam Orozco Oct 18 '16 at 19:03
  • Use die(); at the end of your echo, and consider using type:'json'. What i use personally is ```die(json_encode($data));``` – Andrew Donovan Oct 18 '16 at 19:10
  • Not sure but none of that helps, the only thing that I found out is that if(isset($_POST['function'])) doesnt get called at all, it goes like the text the ajax passed is empty – Tomislav Nikolic Oct 18 '16 at 19:17
  • Also this is what I get from the console handler.php?function=search_ajax&search=A – Tomislav Nikolic Oct 18 '16 at 19:20
  • i would try removing the double quotes from the function key. and remove the datatype. – Nicolas Oct 18 '16 at 19:34
  • I removed the datatype and removed the datatype from it but still... jquery.min.js:2 XHR finished loading: GET "handler.php?function=search_ajax&search=A" and the result is the same – Tomislav Nikolic Oct 18 '16 at 19:36
  • @empiric `dataType:` refers to the response from the server, not the parameters. – Barmar Oct 18 '16 at 19:39
  • BTW, there's no need to put `{}` around the statements in a `case`. – Barmar Oct 18 '16 at 19:41
  • What version of jQuery are you using? Before 1.9.0, the `method:` option was called `type:`. – Barmar Oct 18 '16 at 19:45

2 Answers2

0

It sounds like you're using a version of jQuery before 1.9.0. The method: option didn't exist in the older versions, it was called type:. That's why you're seeing the parameters appended to the URL, because type: "GET" is the default.

So change

method: "POST",

to:

type: "POST",
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

try this:

 $.ajax({  
            url:"handler.php",  
            method:"POST",  
            data:'{"function":"search_ajax", search:search_text}',  
            dataType:"text"
      })
  .done(function(data){
  $('#search_result').val(data);
  console.log(data);
  alert(data);
            } ) ;
Dermot
  • 17
  • 6