...

View Full Version : PHP Variable problem



jhl84
07-05-2007, 11:53 AM
It's a long code so I try to cut it short and go straight to the point. This code is in one page. This page has 2 forms. The first form will post the variable $item_number to the same page if the update button is clicked.

So my problem is in the 2nd form. How do I call $item_number to the 2nd form? From the 2nd form, I want to post $item_number to a page call testpage.php. Pls advice, thanks!



<?php
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {

............
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$item_number = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($item_number != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$item_number;
} else {
$newcart = $item_number;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>

<html><head></head>
<body>
<form action="shoppingcart.php?action=update" method="post" id="cart">
$item_number
</form>

.....

<form action="testpage.php" method="post">
<input type="hidden" name="item_number" value="<?php ?>" />
</form>
</body>
</html>

mlseim
07-05-2007, 01:40 PM
You've got the right idea ....

<input type="hidden" name="item_number" value="<?php ?>" />

Use a hidden form variable

<input type="hidden" name="item_no" value="<?=$item_number?>" />

Then, in the next PHP script, or the same script (whatever you're calling), at the top,
you would capture that variable. Noticed that I did change the
name from item_number to item_no .... use a unique name because
$item_number will be used again by the script.

So, in the script, you bring in your form variable, in this case using POST,
because your form method="post" ...

<?php
$cart = $_SESSION['cart'];
$action = $_GET['action'];
$item_no = $_POST['item_no'];

Now, you can use $item_no wherever you want.

jhl84
07-05-2007, 03:54 PM
problem solve...thanks man!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum