How do I send a field only by ajax? [duplicate]

0

At the moment I'm sending the whole form, I need to send only one html field by ajax, the IdApart is dynamic and comes from the database follows my ajax code and the field I need to send:

AJAX

$(function () {    
        $("#visualizarAparelhos").submit(function (e) {
        e.preventDefault();
        var FormData = $("#visualizarAparelhos").serialize();
            $.ajax({
              type: "POST", 
              url: "administrador.php",
              data: FormData
            }).done(function (data) {
                console.log(data);
            });
        return false;
        alert("Não enviou!!!");
        }); 
    });

FIELD TO BE SENT:

 print "<td><button id='IdAparelho' value='".$id."' type='submit' class='btn btn-primary' data-toggle='modal' data-target='#modalVisualizarComponentes'>".$id."</button></td>";
    
asked by anonymous 05.12.2016 / 19:20

3 answers

0

You can get the value of the entry with jQuery :

$("#visualizarAparelhos").submit(function (e) {
        e.preventDefault();
        var idAparelho = $("#IdAparelho").val();
            $.ajax({
              type: "POST", 
              url: "administrador.php",
              data: idAparelho 
                    ^^^^^^^^^^
            }).done(function (data) {
                console.log(data);
            });
        return false;
        alert("Não enviou!!!");
        }); 
    });
    
05.12.2016 / 19:24
0

As you want to send only the ID of the device, the right would be:

$("#visualizarAparelhos").submit(function (e) {
    e.preventDefault();
    $.ajax({
          type: "POST",
          dataType: 'json',
          url: "administrador.php",
          data: {idAparelho: $("#IdAparelho").val()}
    }).done(function(data) {
        console.log(data);
    });
});
    
06.12.2016 / 01:10
0

Since you are using serialize , you can concatenate the variables like this:

FormData + "&idAparelho=" + idAparelho

An example:

$(function () {    
    $("#visualizarAparelhos").submit(function (e) {
        e.preventDefault();

        var FormData = $("#visualizarAparelhos").serialize();
        var idAparelho = $("#IdAparelho").val();

        $.ajax({
          type: "POST", 
          url: "administrador.php",
          data: FormData + "&idAparelho=" + idAparelho
        }).done(function (data) {
            console.log(data);
        });

        return false;
    }); 
});

I removed alert("Não enviou!!!"); because it did not make sense after return false; , since everything that comes after return ; will never run.

    
05.12.2016 / 20:02