keyup38
keyup38

Reputation: 25

Retrieve data from mysql no output

I am trying to get data from mySQL database without refreshing my page. To get get the data from the user I am using a livesearch also from the same database. When I press the button I want to show the data of the line in the database that the user has inputed. My problem is when I enter the name of a smartphone for example, there is no output I know that the jQuery works fine but I can't see what I have done wrong. Thank you. Here is my code: HTML:

<h1>LIVE SEARCH WITH AJAX TEST</h1>

  <div class="ui-widget">
    <label for="recherche">Recherche: </label>
    <input id="recherche" name="recherche">
    <button name="button" id="button">OK</button>
  </div>
  <br>
  <div id="namedata"></div>

jQuery:

$(document).ready(function(){
    $('#button').on('click', function(){
        var recherche = $('#recherche').val();
        if ($.trim(recherche) != ''){
            $.post('getvalue.php', {recherche: recherche}, function(data){
                $('#namedata').text(data);
            });
        }
    });  
});

PHP:

<?php
if (isset($_POST['recherche']) === true && empty($_POST['recherche']) === false) {
    require 'connect.php'; 

    $query = mysql_query("SELECT capacityingo FROM smartphone WHERE name ='" . mysql_real_escape_string(trim($_POST['recherche'])) . "' "); 

    echo (mysql_num_rows($query) !== 0) ? mysql_result($query, 0, 'capacityingo') : 'Name not found';
}
?>

Upvotes: 0

Views: 51

Answers (1)

Afif Zafri
Afif Zafri

Reputation: 625

Seems like your PHP code is the problem. Try this:

// your query, execute   
$query = mysql_query("SELECT capacityingo FROM smartphone WHERE name ='" . mysql_real_escape_string(trim($_POST['recherche'])) . "' "); 

// fetch the result, into array $row  
$row = mysql_fetch_array($query);

// output the result in JSON format
echo json_encode($row);

After that, you can use the jquery to parse the json data and display it

Upvotes: 1

Related Questions