Sorted list PHP+AJAX

Asked

Viewed 171 times

0

I have been having the following problem since yesterday, putting together an orderly list as I will search for data in the database. I suggested to do with ajax, I did with the help of a colleague of the forum until a certain part. However, in the search with the bank he does not return me anything. Always from Undefined, I believe he’s not looking in the bank.

Follow 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));
?>

2 answers

2


Whereas your query returns results, in principle your problem is that the return of AJAX interpreted by Javascript is a string and you’re treating as JSON.

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

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

or carry out the parse with JSON.parse

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

Any of the solutions should solve your 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 return of PHP is an array similar to this

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

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

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

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

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

Ps. 2: Closing the tag in PHP (?>) it is better to be omitted.

Update 2

Now that you have updated the PHP code, remove the cipher that is at the beginning of PHP and any other characters or whitespace. You must return only the JSON string.

    $ //<-- remova qualquer caracter que esteja antes da abertura de tag do PHP.
<?php
  • This code will run locally, but thanks for the warning. I tried to pass it as Json, now it n of no return haha

  • $('#fetch'). on('click', Function(){ $.ajax({ type: 'GET', url: 'search.php', dataType:"JSON", date: { id: $('input[name=id]'). val() }, Success: Function(data){ $('. table tbody'). append('<tr><td>'+data.name+'</td></tr>') } })

  • @Progmen add the.log(data) console to success and see what the return is on the console. Anyway, add a callback error to check if an error occurs. Another point, is to use firebug (now native to firefox) and check the requests, see what is returning by the ajax request.

  • Could you show lagum support code ? Because I’m a beginner in php, ajax so I’m only using because many people recommended, I don’t know very well

  • <br /> <b>Notice</b>: Undefined variable: return in <b>C: xampp htdocs busca.php</b> on line <b>14</b><br /> null The console returned this.

  • You will have to post the rest of the code busca.php, because, there informs line 14, its code has only 7 lines.

  • There was a small error in the code, now the console returns me exactly the data I want, but still does not show on the screen.

  • The error is in the ajax display, because the data returns correctly, which can be ?

  • I updated the answer, but your PHP changed a lot from the previous example. That dollar sign ($) can’t be there.

  • in which line ? posted the whole php code

  • I’ve updated the entire answer. If the code you’re posting here is the same one you’re testing, it should solve your problem

  • solved! thanks. Now giving other mistakes I have not treated haha, vlw!

Show 7 more comments

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");

This way select will bring the data in an orderly manner.

Explanation:

Using the command order by you will sort the values of a column ascending(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;
  • So Jonathan, the problem is I want to do one search at a time, and not all understand ?

  • In the case of a specific ID?

  • exact. Search the search it would show info of each id

  • There are several scenarios for this. I could describe better what happens when returning and how the query is made.

  • This is for me to generate a "sales report", enter the order ids and display some information of it in the table to be printed. However I am building using the client as a base, because then I change the sql, because the database is not ready.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.