pass the id of an input to another page

0

I have a form that in the inputs field has id and when submitting the form I wanted to retrieve those values in my code.

<form  action="edit.php" method="post"]>
    <label class="" >
     <span class="legend">Requisito 1 :</span>
     <input type="text" name="req1" id="1"value="teste 1" style="margin-bottom: 10px;"></label>

     <label class="" >
     <span class="legend">Requisito 2 :</span>
     <input type="text" name="req2" id="2"value="teste 2" style="margin-bottom: 10px;"></label>
</form>

So when I press submit, on the edit.php page, I retrieve the value of each input this way

$req1 = $_POST['req1'];
$req2 = $_POST['req2'];

Then how do I retrieve the value id of each input and send it to the page edit.php

    
asked by anonymous 17.05.2017 / 03:26

2 answers

2

As commented, the function of the id property is to identify only one element in the DOM, in HTML. If you need this value in PHP, you should not use this attribute. First, because it does not make sense. Second, because this information is not passed through the HTTP protocol for PHP. The correct way is to store the desired value in a form field of type hidden if you do not want it to appear on the page.

<form action="edit.php" method="post">
    <label class="" >
        <span class="legend">Requisito 1 :</span>
        <input type="text" name="req1" value="teste 1" style="margin-bottom: 10px;">
        <input type="hidden" name="req1_id" value="1">
    </label>

    <label class="" >
        <span class="legend">Requisito 2 :</span>
        <input type="text" name="req2" value="teste 2" style="margin-bottom: 10px;">
        <input type="hidden" name="req2_id" value="2">
    </label>
</form>

With PHP, you can retrieve the data with:

$req1 = $_POST['req1'];
$req1_id = $_POST['req1_id'];

$req2 = $_POST['req2'];
$req2_id = $_POST['req2_id'];
    
17.05.2017 / 03:47
0

The value name and id are the same. Inside the edit.php page do the following

<?php 

$req1 = isset($_POST["req1"])?$_POST["req1"]:"";
$req2 = isset($_POST["req2"])?$_POST["req2"]:"";

echo $req1 ." ". $req2;

Teste ai.

?>
    
17.05.2017 / 04:04