Capture Input ID

Asked

Viewed 1,909 times

5

Anyone knows how I do to capture an input id 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 I will use to call it the value I use to add, and the id I want to display in the table when I perform the Insert.

Someone knows how I do it?

  • This input is inside a form?

  • Yes it is inside a form

  • You’ll have to use javascript to catch it and send it to php ... or concatenate value by a delimiter and give it a explode() in php.

  • 2

    No, bro. Connect... the table in the database already inserts a ID automatic if you configure the auto_increment.

  • Thank you very much

3 answers

2


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

if you have multiple checkboxes

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

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

});

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 must be something like:

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

However if you have little knowledge of ajax and have urgency in delivering the project, then recommend to do by pure html like this:

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

PHP must be something like:

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

I do not know how this your html code and not 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 code the logic here applies to "almost anywhere".

2

A possible way without using function attr() is:

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

Or if it’s just one element:

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

The Alert is illustrative.

Browser other questions tagged

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