Page 1 of 1

Passing Hidden Variables in an array

Posted: Mon Nov 02, 2009 9:45 am
by jonjt
Hi, im trying to pass a number of variables with the same name through a form in the following format:

cart.php?action=add&id=20,24&q=1,2

It's a product page and i'm trying to add multiple products to a cart with one button, cart side if i enter the following example in the browser i can explode the array and add the product so i just need to be able to send the correct url.

The page code is as follows:

<?php
$sql = 'SELECT * FROM products WHERE price = 122.33 ORDER BY id';
$result = $db->query($sql);
$output[] = '<form action="cart.php?action=add" method="GET">';
$output[] = '<input type="hidden" name="action" value="add">';

$id = implode(',', $_GET['id']);
$q = implode(',', $_GET['q']);

$output[] = '<input type="hidden" name="id" value="'.$id.'">';
$output[] = '<ul>';
while ($row = $result->fetch()) {
$output[] = '<li>'.$row['name'].' <br> '.$row['code'].' &pound;'.$row['price'].'<input name="q" type="text" value="1" size="3" maxlength="3" class="input" /></li>';
}
$output[] = '</ul>';
$output[] = '<input type="submit" value="Add to Quotation" class="button" />';
$output[] = '</form>';

echo join('',$output);
?>

Thanks J

Re: Passing Hidden Variables in an array

Posted: Mon Nov 02, 2009 10:03 am
by s1auk14
Can you show the html of the echo?

Re: Passing Hidden Variables in an array

Posted: Mon Nov 02, 2009 10:10 am
by jonjt
Here is the complete HTML output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
<link rel="stylesheet" href="css/styles.css" />
</head>
<body>
<div id="shoppingcart">
<h1>Your Shopping Cart</h1>
<p>You have no items in your shopping cart</p>
</div>
<div id="booklist">
<h1>Test</h1>
<form action="cart.php?action=add" method="GET">
<input type="hidden" name="action" value="add">
<input type="hidden" name="id" value="">
<ul>
<li>Trillipse <br> THMA01 &pound;122.33<input name="q" type="text" value="1" size="3" maxlength="3" class="input" /></li>
<li>Trillipse <br> THMA03 &pound;122.33<input name="q" type="text" value="1" size="3" maxlength="3" class="input" /></li>
</ul>
<input type="submit" value="Add to Quotation" class="button" />
</form>
</div>
</body>
</html>

Re: Passing Hidden Variables in an array

Posted: Mon Nov 02, 2009 10:27 am
by s1auk14
Ok. You stated that 'cart.php?action=add&id=20,24&q=1,2' is the link. Do you still need $id = implode(',', $_GET['id']); and this $q = implode(',', $_GET['q']); in your code? Why don't you try only $id = $_GET['id']; and this $q = $_GET['q'];. I think your link already set the comma for you, don't it?

Re: Passing Hidden Variables in an array

Posted: Mon Nov 02, 2009 11:26 am
by jonjt
Hi, thanks for your reply,

The url that get sent currently is:
cart.php?action=add&id=&q=1&q=1

The best i can get to is:
cart.php?action=add&id=51&q=1&id=61&q=1

but only the second id and quanity gets added, the comma aren't added, it just adds new variables.

Re: Passing Hidden Variables in an array

Posted: Mon Nov 02, 2009 8:58 pm
by s1auk14
If you use the link 'cart.php?action=add&id=51&q=1&id=61&q=1', I don't think you can get all the id value using $_GET['id']. It will only get the last id value which is 61. The same with the q. Maybe you can use $_SERVER['QUERY_STRING'] to get all the URL parameters which will be 'action=add&id=51&q=1&id=61&q=1'. Then you do something with them.

Hope this help.

Re: Passing Hidden Variables in an array

Posted: Tue Nov 03, 2009 10:16 am
by AbraCadaver
Exactly. And you shouldn't be using GET to add, update or delete anyway. Use POST. GET is for getting.