Andre
Andre

Reputation: 1

Getting id with $_POST - Is it possible?

Have this form:

<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST" id="form_pesquisa" data-toggle="validator" role="form">
    <div class="radio"> <label> <input type="radio" name="cliente"  id="73"  value="ss">  "ss" </label> </div>
    <div class="radio"> <label> <input type="radio" name="cliente"  id="74"  value="aa">  "aa" </label> </div>
    <button type="submit" class="btn btn-warning" name="hchoose">Choose client</button>

How can I get the id of the radio when the button is pressed? $_POST['id'] does not work. Tks all.

Upvotes: 0

Views: 56

Answers (3)

Arsalan Mithani
Arsalan Mithani

Reputation: 490

The id doesn't get passes to the server for processing, only the input name and value get passed. PHP uses name to process the user input & $_POST['anything'] is used to get the user input of <input type="radio" name="anything" id="73"> & not the id="73". JavaScript can be used to get the id but not PHP actually.

If you really want to get whatever is in the id you can put it in value like value="ssId-73"

Upvotes: 1

Eduardo Escobar
Eduardo Escobar

Reputation: 3389

If you don't want to edit your form (which will be the easiest way), you'll have to use jQuery, because using native Javascript will make it even more difficult.

Below your form:

<script src="jQuery_url">
</script>
<script>
    jQuery("#form_pesquisa").on("submit", function() {
        var id = jQuery("input[name='cliente']:checked").attr("id");
        jQuery(this).append("<input type='hidden' name='id' value='"+id+"'>");
    });
</script>

Having done this, in your form handler file, $_POST['id'] will contain the value you want to catch.

Upvotes: 0

Wolen
Wolen

Reputation: 904

You have to set name for the button eg. <input type="radio" name="myRadioButton"/> then in PHP $_POST['myRadioButton']

There's no way to receive id in PHP, you can only set same name as id.

Upvotes: 0

Related Questions