How to pass a value to next page with PHP

Asked

Viewed 17,853 times

0

In the code below I try to pass the value to the next page NumeroCartao. But the variable is empty on the next page. How to 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>

PHP QUERY.

$numeroCartao = $_POST['NumeroCartao'];
echo "NUMERO CARTA0 {".$NumeroCartao."}";
  • You can post the code that is between <form> and </form>?

  • The echo $linha['NumeroCartao'] displays the value correctly?

  • No use <form> ajax use!

1 answer

8


On the method of form:

When sending data from a form on PHP, the usual methods are GET and the POST. Variables sent with the GET vain in the URL request, as in the following example:

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

and shall be recovered in the PHP with $_GET['NumeroCartao'].

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

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

<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 the form, but you can take advantage and use simple quotes in this case:

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

With double quotes, the character { has a special function in the PHP to interpret variables, better always avoid ambiguities.


Although the PHP allow this format:

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

give preference to the long version whenever you can:

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

Thus, your application will become more portable if one day it goes to a server where the short tags <? are disabled.

Browser other questions tagged

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