How to send 2 arrays/parameters through the url?

Asked

Viewed 1,298 times

0

I am trying to send two parameters of the shopping cart through the url ,for later the client to make the confirmation, but I can only receive an array on the other page and do not know how to receive two.

I want to send the quantity of products and the id_products to another page, where the user will confirm and then enter database.

On the cart page

<?php
**// array para guardar todos os valores que estão no carrinho
$emparray[] = $product['id_produto'];
$quant[] = $product['quantidade'];
...

// transformar a array numa associação de parâmetros para a url
$query = http_build_query($emparray);
testar = http_build_query($quant);

$cart_box .= '<div class="cart-products-total">Total : '.$total.' € <u><a href="index.php?page=confirmar_encomenda&'.$query.'&'.$testar.'"  title="Verifica o carrinho e faz checkout">Concluir</a></u></div>';

?>

On the other page, this is how I receive the values. In order to send in the form confirmation.

<?php
$teste=$_GET;
echo $teste;

foreach($teste as $value){
   echo' <input type='text" name="result[]" value="'. $value.'">';
}
?>

But this way he only shows the last array and I’ve tried N ways to receive the two separately but could not.

  • Hello, you are badly constructing the array, passing the parameters, and also overloading the reception, read this here.

1 answer

2


Two things:

  1. Avoid passing this type of purchase data by GET, use the method POST. Besides being safer, there’s a limit on the amount of characters you can pass through GET 2083 characters. It seems a lot, but if your customer is buying 100 different products it may well exceed this limit.

  2. When you define the arrays that will go through http_build_query you are passing the value directly, so the resulting array should be arrays with numbered rather than associative indices, so the GET must be overwriting the parameters you are passing to it, i.e.: the result in the URL must be more than one parameter with the same key: ?page=confirmar_encomenda&0=1344&0=2. Instead of using two arrays, why not pass the products array that contains all the information?

$query = http_build_query($product);

  • addition of products in a trolley, can be safely done using GET, since it’s just add, and if the site has theft account protection, it will be as safe as breathing - or not. But you see the idea right away, don’t you? Taking advantage of the question, read this here, maybe you understand better what http_build_query.

  • And on the following page how I separate the data, in case I send everything?

  • The name of the properties on $_GET it’s going to be different. So $_GET['quantidade'] will bring the quantity of products. Or $_POST if you prefer.

Browser other questions tagged

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