1

I am trying to use a code that was flagged as working and I am not getting any results, it's just empty. Notes: The MySQL does return results if run alone, so it's not an sql thing. Any suggestions?

HTML:

<head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">       
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>
    <body>

        Name: <input id="hint" />

        <script type="text/javascript">

            $("#hint").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "getMedicineNames.php",
                        dataType: "jsonp",
                        data: {
                            name: request
                        },
                        success: function (data) {
                            response(data);
                        }
                    });
                },
                minLength: 3
            });

        </script>
    </body>

PHP:

require 'connect.inc.php';
$mysql = mysqli_connect("$db_host", "$db_username", "$db_pass", "$db_name");
$name = $_POST['name'];
if ($name != "") {

    $sql = "SELECT MedicineName FROM medicinetypes WHERE MedicineNAme LIKE '%$name%'";
    echo json_encode(mysqli_fetch_all(mysqli_query($mysql, $sql), MYSQLI_ASSOC));
}
4
  • 3
    correct this line dataType: "json", not the dataType: "jsonp", Commented Aug 1, 2017 at 11:11
  • also include type : "POST", Commented Aug 1, 2017 at 11:13
  • still nothing.. Commented Aug 1, 2017 at 11:13
  • can you add your response of ajax
    – MyTwoCents
    Commented Aug 1, 2017 at 11:20

3 Answers 3

0

Please add dataType: "json", & type : "GET".

If not working please add your screenshot of JSON response.

2
  • ok so changed a few thing, first it's request.term then and php now accepts get. The data is successfuly returned and shown on the console with console.log(data) but it stops there, there is nothing happening on the front-end Commented Aug 1, 2017 at 11:39
  • now i made something happen but some empty tiny fields appear Commented Aug 1, 2017 at 11:51
0

Bilal Ahmed answer is correct.

Aditinoally this is a bit incorrect:

$name = $_POST['name']; // Maybe cause a PHP Warning

You can solve this creating a unset value:

$name = isset($_POST['name']) ? $_POST['name'] : "";
/* "" can be NULL, FALSE, 0 or any value */

In PHP7 you can make this:

$name = $_POST['name'] ?? "";
/* "" can be NULL, FALSE, 0 or any value */
0

Alright I found a better answer, but the main problem was my php code where I left the array as an assoc which is wrong.

JS:

$(function () {
    $("#hint").autocomplete({
        source: 'getMedicineNames.php'
    });
});

PHP:

$mysql = mysqli_connect("$db_host", "$db_username", "$db_pass", "$db_name");
$name = $_GET['term'];

$sql = "SELECT MedicineName FROM medicinetypes WHERE MedicineNAme LIKE '%$name%'";
$res = mysqli_query($mysql, $sql);
$res = mysqli_fetch_all($res, MYSQLI_ASSOC);

foreach($res as $row){
    $data[]=$row['MedicineName'];

}
echo json_encode($data);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.