Ordered list PHP + AJAX

0

I've been having the following problem since yesterday, putting together an ordered list as I'll be fetching data in the database. I was suggested to do with ajax, I did with the help of a colleague of the forum to a certain extent. However, in the search with the bank it does not return anything to me. It's always undefined, I do not think it's looking at the bank.

Follow the code in ajax and php

AJAX Code

$('#buscar').on('click', function(){
    $.ajax({
      type: 'GET',
      url: 'busca.php',
      data: {
        id: $('input[name=id]').val()
      },
      success: function(data){
        $('.table tbody').append('<tr><td>'+data.nome+'</td></tr>')
      }
    })
    return false
  })

PHP code

    $
<?php


    $host='localhost:C:/bd_relatorio/clipp.FDB';
    $dbh=ibase_connect($host,'SYSDBA','masterkey');
    $stmt = 'SELECT NOME, ID_CLIENTE FROM TB_CLIENTE';
    $sth = ibase_query($dbh, $stmt);


    $id = $_GET['id'];
    $sql = ibase_query("SELECT NOME FROM TB_CLIENTE WHERE ID_CLIENTE LIKE '%".$id."%'");
    $row = ibase_fetch_row($sql);
    die(json_encode($row));
?>
    
asked by anonymous 18.10.2017 / 17:47

2 answers

2

Considering that your query returns results, in principle, your problem is that the AJAX rendering interpreted by JavaScript is string and you are treating it as JSON .

To resolve this, add the property dataType='JSON' to $.ajax :

$.ajax({
    method: "GET",
    dataType: "JSON", //<-- define que o tipo de retorno será tratado como JSON
    // restante do código

or parse with JSON.parse

var json = JSON.parse(data);
console.log(json.nome);

Either solution should solve your problem with undefined .

Ps: Your code is vulnerable to SQL Injection. . Further reading

Update

There is another situation to be addressed. When you are using the following code snippet:

ibase_fetch_row($sql);

The PHP return is an array similar to this

array(1) { 
    ["nome"]=> string(14) "Gabriel Heming" 
}

Soon after, you're adding to the $retorno['NOME'] variable. That is, your array has become this:

array(1) { 
    ["nome"]=> array(1) { 
        ["nome"]=> string(14) "Gabriel Heming" 
    } 
}

Therefore, the NOME index is not necessary. You can do it exactly like this:

$retorno = ibase_fetch_row($sql);       
echo json_encode($retorno);

Ps. 2: Closing the tag in PHP ( ?> ) is best left out.

Update 2

Now that you have updated your PHP code, remove the dollar sign that is at the beginning of PHP and any other character or white space. It should only return the JSON string.

    $ //<-- remova qualquer caracter que esteja antes da abertura de tag do PHP.
<?php
    
18.10.2017 / 17:56
0

Just use the command SQL Order By:

$sql = ibase_query("SELECT NOME FROM TB_CLIENTE WHERE ID_CLIENTE LIKE '%".$id."%' ORDER BY ID_CLIENTE ASC");

In this way select will bring the data in an orderly way.

Explanation:

Using the command order by you will sort the values of a column upwards (ASC) or descending (DESC).

SELECT "nome_coluna"
FROM "nome_tabela"
[WHERE "condição"]
ORDER BY "nome_coluna" [ASC, DESC];

Example 1:

SELECT Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;

Example 2:

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
    
18.10.2017 / 18:03