How do I send a field just by ajax?

Asked

Viewed 401 times

0

At the moment I am sending the entire form, I need to send only one field of html by ajax, Idaparelho 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>";
  • 1

    Roberto, this "id" that you are using, is being the same for each line ? type, you have several Uttons with id='Idaparelho' and only changes the value ?

  • No, this id is dynamic, it comes from the database, I just want to get the value of the button I press.

  • right, then you won’t have another element with the same id ='IdAparelho' ? I ask this because it is a common mistake to create several lines changing only the value and keeping all id equal (which does not work because ids need to be unique)

  • Actually Lucas, I hadn’t thought about it, I get your logic, I’ll sort that question out too, I’ll do Idaparelho+$Id

  • @Robertoalbino saw my answer?

3 answers

0

You can get the value of the input 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!!!");
        }); 
    });
  • I changed my ajax with your code, and now it is returned the following error : jquery.min.js:5 Uncaught Rangeerror: Maximum call stack size exceeded

  • 2

    Infinite loop, young man.

  • Would not be data: "idAparelho=" + idAparelho?

  • "need to send only one html field by ajax", I understood that it wants to send only one field, and it is a post, I do not understand much of PHP, but is usually used parameter with get.

  • But the .val does not send the "field" it sends the value of the field :/

  • That’s right, but why would it be interesting to send the field and the value, if it wants to send only one field?

  • To get a value in the POST you need a key and a value, in case you are only sent the value, that’s what I meant, your example is only sending the value and does not have an associative key to identify within the $_POST. In fact in your example the value becomes a key, which makes it more difficult to obtain

Show 2 more comments

0

Since you’re wearing it 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 the alert("Não enviou!!!");, because it didn’t make sense after the return false;, since everything that comes after return ; will never be executed.

0

Since you only want to send the device ID, the right one 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);
    });
});

Browser other questions tagged

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