pass the id of an input to another page

Asked

Viewed 1,104 times

0

I have a form that we inputs has the field id and when sending the form I wanted to recover these 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 send, on the page Edit.php, recover the value of each input in that way

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

So how do I recover the value id of each input and send to the page edit.php

  • Question: why do you need id of the element? Its function is to identify only one element in the DOM, in the HTML. If you need this value in PHP, you are implementing it wrong.

  • because on the Edit page I give an upty in the table where the id coming from the input is equal to that of the bank

2 answers

2


As commented, the function of the property id is to identify only one element in DOM, in HTML. If you need this value in PHP, you should not use this attribute. First, because it doesn’t 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 recover the data with:

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

$req2 = $_POST['req2'];
$req2_id = $_POST['req2_id'];
  • very good hadn’t thought to do it that way

0

The value name and id are the same. Within 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.

?>
  • Thus the value in value of input. The question is on how to get the value in id. I don’t think you understand very well what you’re asking.

  • True, he wanted the id name="Here" of the input.

Browser other questions tagged

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