Problem sending data with $ .ajax ()

0

Thanks in advance to all who can help me with this doubt. I'm just starting with JQUERY and I'm trying to make a form that should do some calculations based on data that returns a db with a query in external file in php. Based on everything I have searched and read, I have the following file in PHP that makes the query (It is proven and returns data). (turi.php)

And this is the jquery code, which I ask you to activate when changing a select

$(document).ready(function() {
$('#destinos').change(function() {
        $.ajax({
          type: "POST",
          dataType: 'json',
          data: {filtro: "FsL", limit: 15},
          url: "turi.php",
          success : function(resp) {
            // Aquí debería poder recorrer el resultado que devuelve la consulta... creo :)
          },
          error : function(resp) {
            alert("Error");
          }

      })
})});

To have me return an object and be able to go through it. And based on other data that the user loads, I do a calculation.

I hope it's clear.

Again, Thank you

    
asked by Soluciones PyMES y Hogares 04.12.2017 в 21:05
source

2 answers

1

The result of the data processed by php is what you get in the parameter resp that you are passing to the event success of ajax , to traverse that object json you could use the method $.each() of jQuery :

$(document).ready(function() {
    $('#destinos').change(function() {
        $.ajax({
            type: "POST",
            dataType: 'json',
            data: {filtro: "FsL", limit: 15},
            url: "turi.php",
            success : function(resp) {
                $.each(resp, function(index, dato){
                    console.log(dato.propiedad);
                });
            },
            error : function(resp) {
                alert("Error");
            }
        });
    });
});

$. each ()

This method is used to run any type of data collection (object or array), as you can see the first parameter is the collection that you want to go through, and the second parameter is a callback that is executed for each repetition performed. This callback receives two parameters that are mandatory:

index : is the index / name

dato : is the object / value

Really to these two parameters you can put the name you want, the important thing is that they are both.

I hope I have been clear, greetings!

    
answered by 04.12.2017 / 21:31
source
1

more or less it should be like this

replace the names and you're done

$("#boton_que_envia_los_datos").click(function(){
var url = "documento.php";

$.ajax({
        type:"POST",
        url: url,
        data: $("#Id_de_tu_formulario").serialize(),
        success: function(data)
        {                         
          $("#id_del_elemento_donde_recibiras_la_respuesta").html(data); 
        }
 });
}

in the HTML form that you will send

<form class="form-horizontal" id="Id_de_tu_formulario" method="post">

<input type='text' name='txt1'  id='txt1' >


 <button type="submit" id="boton_que_envia_los_datos" name="boton_que_envia_los_datos">Enviar</button>

</form>

<div id="id_del_elemento_donde_recibiras_la_respuesta"></div>

in the php document you will receive the data

<?php

print_r($_POST);



?>
    
answered by 04.12.2017 в 21:17