How to pass a value to the next page with PHP

0

In the code below I try to move to the next page with the value NumeroCartao . However, the variable is empty on the next page. How do I fix this error?

INDEX.PHP

<label> Numero da Carteira: </label>  <span> <? echo $linha['NumeroCartao'];?> </span>
<div class="">
   <input class="numerocarteira" id="NumeroCartao" name="NumeroCartao" type="hidden" value="<?=$linha['NumeroCartao']; ?>" />
   <input class="" id="NumeroCartaoDis" name="NumeroCartaoDis" type="text" disabled="" value="<?=$linha['NumeroCartao']; ?>" />
</div>
</label>

CONSULTA.PHP

$numeroCartao = $_POST['NumeroCartao'];
echo "NUMERO CARTA0 {".$NumeroCartao."}";
    
asked by anonymous 30.04.2014 / 01:44

1 answer

8

About method form :

When sending data from a form in PHP , the usual methods are GET and POST . The variables sent with GET go to the URL of the request, as in the following example:

http://example.com/consulta.php?NumeroCartao=2890127812781233

and should be retrieved in PHP with $_GET['NumeroCartao'] .

Already sent with POST , go in the body of the request, and do not appear in URL . These must be recovered $_POST['NumeroCartao'] .

For your code, you need to make sure that you are using the POST method

<form action="consulta.php" method="post">
   <label>Numero da Carteira:</label>  <span><?php echo $linha['NumeroCartao'];?></span>
   <div>
      <input class="numerocarteira" id="NumeroCartao" name="NumeroCartao" type="hidden" value="<?php echo $linha['NumeroCartao']; ?>" />
      <input id="NumeroCartaoDis" name="NumeroCartaoDis" type="text" disabled="" value="<?php echo $linha['NumeroCartao']; ?>" />
   </div>
</form>

Other considerations:

Not directly linked to form, but you can take advantage of and use single quotation marks in this case:

echo 'NUMERO CARTA0 {'.$NumeroCartao.'}';

With double quotation marks, the { character has a special function in PHP to interpret variables, but rather to avoid ambiguities.


Although PHP allow this format:

<?= $linha['NumeroCartao']; ?>

Give preference to the long version whenever you can:

<?php echo $linha['NumeroCartao']; ?>

So your application will become more portable if you ever go to a server where the <? short tags are disabled.

    
30.04.2014 / 01:56