0

I'm trying to get data from mysql database using ajax and restapi in php. I wanted that I inserted a value on a search bar and it shows me the data from mysql

api code:

        $response = array();
        $posts = array();
        $ID = $_POST['ID'];
        $sql = "SELECT * FROM table WHERE client_id = ?";
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param("s", $ID);
        $stmt->execute();
        $result= $stmt->get_result();
        $row = $result->fetch_assoc();
        while($row=mysql_fetch_array($result)) { 
          $user_id=$row['id'];
          $username=$row['username'];   
          $client_id=$row['client_id']; 
          $token=$row['token'];  

          $posts = array('id'=> $user_id, 'username'=> $username, 'client_id'=> $client_id, 'token'=> $token);
        } 

        echo json_encode($posts);

        $stmt = null;   
        $mysqli = null;
    }

?>

And this is the ajax code

$('document').ready(function()
                { 
                    $("#search").on("click", function(e){
                        e.preventDefault();
                        var to_search = $("#search_value").val();
                        console.log(to_search);

                        var formData = {
                        'ID' : to_search,
                      };

                      $.ajax({
                        type : 'POST',
                        url : 'search.php',
                        data : formData,
                        dataType : 'JSON',
                        encode : true,
                        success: function (data, response, status, xhr) {
                          if (response.result) {
                            console.log('done');
                            console.log(data);
                          }else{
                            console.log('fail');
                            console.log(data);
                          }
                        },
                        error: function (xhr, status, error) {

                        }
                                });              
                             });    
                  });

It's not working, I would just like that it print the result on the browser console. The button is working because it shows on the console the value that I inserted on the search bar Any advices?

2
  • 1
    add a console.error(error) in the error method and see what is going wrong Commented Jan 31, 2018 at 16:21
  • It show nothing, just printed the result of this "console.log(to_search);" Commented Jan 31, 2018 at 16:26

1 Answer 1

1

I don't see any problem in your ajax call, however i did some amendments to your PHP code (this is untested).

    $response = array();
    $posts = array();
    $ID = isset($_POST['ID']) ? $_POST['ID'] : 0; // Avoid warning because of missing "ID" post field
    $sql = "SELECT * FROM table WHERE client_id = ?";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('i', $ID); // ID's probably an integer, not a string
    $stmt->execute();
    $result= $stmt->get_result();
    // Removed $row = $result->fetch_assoc();
    while($row = $result->fetch_assoc()) { // Better use it this way
        $user_id = $row['id'];
        $username = $row['username'];   
        $client_id = $row['client_id']; 
        $token = $row['token'];  

        array_push($posts, array('id'=> $user_id, 'username'=> $username, 'client_id'=> $client_id, 'token'=> $token)); // Add the current loop element data to $posts
    } 

    echo json_encode($posts);

    $stmt = null;   
    $mysqli = null;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I did the changes you and @Anatolii Bivol told me at least now show the error, prntscr.com/i88omo Sugestions?
@ShwSvn Are you sure you are passing an ID that exists in the table for it to return? You will get that result when it matches nothing in the table.
Okay it was my fault, I got the array with the data now In that question, how can I show the data I recived on the ajax, to show on html
@IncredibleHat It was a problem in my query yes, thank you

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.