Capture Input ID

5

Does anyone know how I can capture the id of an input and send it to the database?

EXAMPLE

I have this input:

<input type="checkbox" name="adicional" id="Leite Ninho" value="2.00">

It has the name that I will use to call it the value that I use to add, and the id that I want it to display in the table when I perform the insert.

Does anyone know how I can do this?

    
asked by anonymous 30.10.2015 / 16:49

3 answers

2
$("input[name=adicional]").attr("id")

If you have multiple checkboxes

$("input[name=adicional]").each(function(){

//var ou array 
$(this).attr("id");

});
    
30.10.2015 / 18:10
2

You can use Ajax with jQuery:

var field = $("input[name=adicional]");//Pega o seu campo

$.ajax("pagina.php", {
    "type": "POST",
    "data": {
        "id": field.attr("id"), //Envia o id
        "adicional": field.val() //Envia o valor
    }
}).done(function(data) {
    alert(data);
}).fail(function(a, b) {
    alert(b);
});

PHP should be something like:

<?php
$id    = $_POST['id'];
$valor = $_POST['adicional'];

However if you have little knowledge of ajax and have an urgency to deliver the project, then I recommend doing it by pure html like this:

<input type="hidde" name="adiciona-id" value="Leite Ninho">
<input type="checkbox" name="adicional" value="2.00">

PHP should be something like:

<?php
$id    = $_POST['adiciona-id'];
$valor = $_POST['adicional'];

I do not know how your code is html or php, because this you did not put in the question, I answered what is inside what was asked, but I believe that regardless of the logic code here applies to "almost anywhere ".

    
30.10.2015 / 18:27
2

One possible way without using attr() function is:

$(":checkbox").each(function(){
    alert( this.id )
});

Or if it's just an element:

alert( $(":checkbox")[0].id )

The alert is illustrative.

    
05.11.2015 / 05:00