Query the return of an Ajax

Asked

Viewed 946 times

1

I have a field select and need to query and load the result dynamically with Ajax. I currently have the following code (which does not appear to work or to be incomplete):

$('#formProd select').change(function(){

    $.ajax({
        type : 'POST',
        url  : 'index.php/home',
        data : {id : $('#formProd select option:selected').val()}
    });                     
});

How do I get this id to make me mine query?

I want to, by selecting an option from select, that on the same page he takes the value of select and make a query in my database to bring me the results.

Vide example.

When clicking on a category, I want you to click on a list of products.

  • details your question. Tell us in detail what you want to do. For example: where do you want to load the result? What is happening in your query above that you said is not working or incomplete? Things like that

  • 1

    Nesse php quando vc imprimir o $_POST he comes empty?

  • 1

    If the question is: "Como eu recebo essa id pra que eu faça minha query?" Simply use $_POST['id'] inside the php file 'index.php/home'.

  • No Paulo Roberto, this did not work. I want to know if the ajax I did is correct.

  • Right it is, the argument id will filled by looking for firebug.

2 answers

4

If the problem is sending the ID of the option selected via Ajax, the code below should work smoothly:

$('#formProd select').change(function(){
    $.ajax({
        type: 'POST',
        url: 'index.php/home',
        data: { id: $(this).val() },
        //contentType: 'application/json; charset=utf-8',
        //dataType: 'json',
        success: function (data) {
            // utilizar o retorno
        }
    });
});

Note that to fill in the value of the ID you want to send you don’t need to access the value this way: $('#formProd select option:selected'), as you have already accessed the component via the "change" event, just use the $(this).val() to access the ID value of the selected option.

  • About the this, truth, it becomes more practical.

  • But why are you specifying that it’s a json? OP didn’t say that in the question.

  • 1

    Just by custom, I will edit it. And also, from what I read of the above comments, it seems to me that the question was more of sql than ajax.

  • It worked here. The question was not about SQL rs. It was about how to get the return of Ajax, because I do not know how to know if it is returning something. But I’ve solved it here based on these tips.

  • 1

    Good! I hope I helped.

3


Your ajax does nothing when completing, but you must do an action that would fill your list with the returned elements in this way for example:

$.ajax({
    type : 'POST',
    url  : 'index.php/home',
    data : {id : $('#formProd select option:selected').val()}
}).done(function() {
  var len = data.id.length; //assumindo que vai retornar um Array.
  var List = []; //lista de elementos retornados
  for(var i=0; i < len; i++){ //percorre a lista de elementos retornados
    List.push("<li>"+data.id[i]+"</li>"); 
  }
  $('#linkList').html(List.join('')); //coloca as li's no ul com os elementos retornados.
});

About the Query you want to do, you can do something like:

$id = $_POST['id'];
if (is_numeric($id))
  $sql = 'SELECT * FROM sua_tabela WHERE id='.$id;

Note that I first checked whether the return of $_POST['id'] is number, so that there is no SQL Injection in your application.

  • Got it Paul. But I just want to do a query like: SELECT * FROM table WHERE id = to that id I want to recover. I just don’t know if I do it by js or in php page.

  • 2

    In JS you do not query... the Select query is the task of php... there in mysql_query("select * from table Where id = 6"); for example...

  • Yes, for sure @tchicotti however the OP did not define the question very well, he meant how to make my ajax update my list (I think...)

  • @Pauloroberto I understood, that’s why in the comment of the question I asked for a better explanation, but responding to his comment his answer, I thought it was what would have more to do

Browser other questions tagged

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