Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP Variable problem

    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!

    Code:
    <?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>
    Last edited by jhl84; 07-05-2007 at 11:54 AM. Reason: typos

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    problem solve...thanks man!
    Last edited by jhl84; 07-05-2007 at 04:06 PM. Reason: problem solved


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •