Is it possible to return 2 values in an Ajax?

1

I have the following script:

('#AjaxUpdateClient').submit(function(e){
    var id = $(this).attr("id");

    var form = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "/updateClient",
        data: form,
        dataType: "json",
        success: function(){
        }
    });
});

It will be used to update a database in the database, but I'll need it to send POST and also id to var id , is there any way that data: returns form, id ? Thanks!

EDITED

On the server side I have an update method, which will get the $ _POST in the variable $params I wanted to receive the id separated from the $_POST so I pass the second method parameter, tried to receive the id next to the $ _POST but it did not work ..

//Update on database
public function update($params, $id)
{
    $params_fields = "'".implode("'= ?, '", array_keys($params))."'= ?";

    $query  = "UPDATE '{$this->table}' SET {$params_fields} WHERE 'id'= ?";
    $stmt   = $this->db->prepare($query);

    $i=1;
    foreach($params as $key => $value){
        $stmt->bindValue($i, $value);
        $i++;
    }
    $stmt->bindValue($i, $id);

    $stmt->execute();
    return $stmt;
}
    
asked by anonymous 22.04.2017 / 17:41

2 answers

1

You have several alternatives, one is to join the ID to ..serialize() , another is to send an object with two keys.

The .serialize() generates a _string de chaves e valores, tipo _querystring_ onde as chaves e valores são aglomerados por & . Assim juntando & id = teuID 'you send what you want to PHP. It could look like this:

$('#AjaxUpdateClient').submit(function(e){
    e.preventDefault();
    var id = this.id;
    var form = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "/updateClient",
        data: form + '&id=' + id,
        dataType: "json",
        success: function(res){
            console.log(res);
        }
    });
});

The other alternative would be to use .serializeArray() that generates an array of objects. And then in jQuery you could use it like this:

$('#AjaxUpdateClient').submit(function(e){
    e.preventDefault();
    var id = this.id;
    var form = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "/updateClient",
        data: {form: form, id: id},
        dataType: "json",
        success: function(res){
            console.log(res);
        }
    });
});
    
22.04.2017 / 19:13
0

I'll give you two functional forms:

  • Pass id by method GET , adding value in action of form :
  • Form:

    <form action="pagina.php?id=123" method="post">
        <!-- [...] -->
    </form>
    

    Script:

    var dados = $( '#MeuFormulario' ).serialize();
    
    $.ajax({
        type: "POST",
        url: $('#MeuFormulario').attr('action'),
        data: dados
    });
    
  • Pass id by method POST , adding inputs of type hidden :
  • Form:

    <form action="pagina.php" method="post">
        <input type="hidden" name="id" value="123">
        <!-- [...] -->
    </form>
    

    Script:

    var dados = $( '#MeuFormulario' ).serialize();
    
    $.ajax({
        type: "POST",
        url: $('#MeuFormulario').attr('action'),
        data: dados
    });
    

    Note that the script is the same, and url is itself action of form . And in the page that will treat the data, it is only usually to receive $_GET['id'] / $_POST['id'] , according to the method used.

        
    22.04.2017 / 19:48