Can I return 2 value in an Ajax?

Asked

Viewed 799 times

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(){
        }
    });
});

will be used to update a register in the database, however I will need it to send the POST and also the id for var id, there is some way data: returns the form, id? thanks!

EDITED

On the server side I have an update method, which will receive $_POST in the variable $params I wanted to receive the id separate from the $_POST for me to pass the second parameter of the method, I tried to get the id together with $_POST but it didn’t 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;
}
  • When you say "the captured id" you mean to var id = $(this).attr("id");?

  • 1

    Either the question is wrong, or I don’t understand. The parameter data does not return anything, just sends. What returns is the json, and in it, you can return whatever you want, in the script that runs in /updateClient.

  • Exactly "send", and yes the id captured in var id.

  • Can you explain what kind of information you send and what you need to know about the server? Do you already have any server part code?

  • I updated the question see if I can be more specific.

  • You could change the url so that it contains the id, updateClient/21, for example, and on the server get the url id.

Show 1 more comment

2 answers

1


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

The .serialize() generates a _stringde chaves e valores, tipo _querystring_ onde as chaves e valores são aglomerados por&. Assim juntando&id=teuID` send what you want to PHP. It could be 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 using the .serializeArray() which 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);
        }
    });
});

0

I’ll give you two working forms:

  1. Pass the id by the method GET, adding the 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
});

  1. Pass the id by the method POST, adding inputs of the kind 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 the url is the very action of form. And on the page that will process the data, just receive normally with $_GET['id'] / $_POST['id'], according to the method used.

Browser other questions tagged

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