pass multiple variables through the GET url

Asked

Viewed 1,504 times

0

I want to pass two variables to another page through GET. I already have one working like this:

 <a class='fa fa-remove' href='remover.php?remover=carrinho&id=$idproduto' style='font-size:24px'></a>

But what I’m doing now is a form so the url would be the form action, and so it’s not working because it sends the form variables and not the ones I wrote in the action, for example:

 <form action='quant.php?qtd=carrinho&id=$idproduto'>
 <div class='input-group mb-2' style='text-align: 
      center'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input class='form-control' id='qtd1' name='qtd2'type='text' 
      value='$quantidade'/>
  <div class='input-group-append'>
<div class='input-group-append'>
<button class='btn btn-outline-secondary fa fa-refresh' type='submit'><span 
     class='glyphicon glyphicon-refresh'></span> 
 </button>&nbsp;&nbsp;&nbsp;&nbsp;
  </div>
 </div>    
   </div>     
   </form>

should go to quant.php?qtd=carrinho&id=$idproduto but goes to quant.php?qtd2=12

I want it to pass the $idproduct and the value the user entered in the inputbox.

  • 1

    The form sends the information inside it. The action is the reference for who will process the data, you would have to create an Hidden input to send this information together.

1 answer

2

This combination of passing values by url and form at the same time do not think it works.

The only way I know is to put the values of the variables inside inputs hidden to pass via get

<form action='quant.php' method="get">
    <div class='input-group mb-2' style='text-align: center'>
    <input name='qtd' type='hidden' value='carrinho'/>
    <input name='id' type='hidden' value='<?php echo $idproduto ?>'/>
        <input class='form-control' id='qtd1' name='qtd2'type='text' value='<?php echo $quantidade ?>'/>
            <div class='input-group-append'>
                <div class='input-group-append'>
                    <button class='btn btn-outline-secondary fa fa-refresh' type='submit'>
                    <span class='glyphicon glyphicon-refresh'></span> 
                    </button>
                </div>
           </div>    
   </div>     
</form>

PHP

$qtd = $_GET['qtd'];

$id = $_GET['id'];

$qtd2 = $_GET['qtd2'];

Or

foreach ( $_GET as $chave => $valor ) {
  // $$chave cria as variáveis com os names dos elementos do formulário
  $$chave = trim( strip_tags( $valor ) );
}

echo $qtd;

echo $id;

echo $qtd2;

Browser other questions tagged

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