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'].' £'.$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
Passing Hidden Variables in an array
Moderator: General Moderators
Re: Passing Hidden Variables in an array
Can you show the html of the echo?
Re: Passing Hidden Variables in an array
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 £122.33<input name="q" type="text" value="1" size="3" maxlength="3" class="input" /></li>
<li>Trillipse <br> THMA03 £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>
<!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 £122.33<input name="q" type="text" value="1" size="3" maxlength="3" class="input" /></li>
<li>Trillipse <br> THMA03 £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
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
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.
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
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.
Hope this help.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Passing Hidden Variables in an array
Exactly. And you shouldn't be using GET to add, update or delete anyway. Use POST. GET is for getting.McInfo wrote:PHP Manual: How do I create arrays in a HTML <form>?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.