riddhi Posted April 23, 2007 Share Posted April 23, 2007 I am a script budget.php which has the following section of the code:- while ($record=mysql_fetch_array($res)){ if($record['PD_PRICE'] <= $price) {// Display the Product if within the Price Range $productId[$indx++]=$record['PD_ID']; I wish to pass on the array $productId to another script add_cart.php which is called as follows:- echo "<form name='form1' method='get' action='add_cart.php'>"; echo "<input name='cart' type='submit' value='Add To Cart' />"; How to go about it? Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/ Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 I am a script budget.php which has the following section of the code:- while ($record=mysql_fetch_array($res)){ if($record['PD_PRICE'] <= $price) {// Display the Product if within the Price Range $productId[$indx++]=$record['PD_ID']; I wish to pass on the array $productId to another script add_cart.php which is called as follows:- echo "<form name='form1' method='get' action='add_cart.php'>" . "<input type=\"hidden\" name=\"productId\" value=\"$productID\">" . "<input name='cart' type='submit' value='Add To Cart' />"; You can then use the variable as $_GET['productId'] in the second page Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236131 Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 To pass it using GET you would need the array serialized www.php.net/serialize www.php.net/unserialize An easier way to do it would be to use SESSION. Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236140 Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 Or you can use the above code with a POST instead of a GET...would probably be easier... Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236142 Share on other sites More sharing options...
riddhi Posted April 23, 2007 Author Share Posted April 23, 2007 I have used session_register("productId") without sucess. As far as serialisation is concerned i could not understand it. :'( please write the code how to access $_GET[productid] in the forloop traversal? Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236145 Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 Well to use it as a session you would do this... session_start(); $_SESSION['productId']=$productId; Then on the next page you would just add a session_start(); and use the variable name $_SESSION['productId'] to get the value... Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236149 Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 <?php if (!isset($_GET) || !isset($_POST)) { $arraySer = serialize($array); print '<input type="hidden" name="productids" value="' . $arraySer . '" />'; }else { $array = unserialize((isset($_POST['productids']))?$_POST['productids']:$_GET['productids']); print_r($array, true); } ?> Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236150 Share on other sites More sharing options...
riddhi Posted April 24, 2007 Author Share Posted April 24, 2007 tried both of them without any suces :'(. Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-236741 Share on other sites More sharing options...
riddhi Posted April 25, 2007 Author Share Posted April 25, 2007 Any body can help me out?? Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-237853 Share on other sites More sharing options...
btherl Posted April 25, 2007 Share Posted April 25, 2007 Try with this little alteration: <?php if (!isset($_GET) || !isset($_POST)) { $arraySer = urlencode(serialize($array)); print '<input type="hidden" name="productids" value="' . $arraySer . '" />'; }else { $arraySer = (isset($_POST['productids']))?$_POST['productids']:$_GET['productids']; $arraySerDec = urldecode($arraySer); $array = unserialize($arraySerDec); print_r($array, true); } ?> If it doesn't work, try inspecting the HTML source. Also try printout out the variables to see if they look sane. Actually, urlencode() is not necessary .. there's a more appropriate function but it slips my mind right now.. maybe htmlentities() Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-237943 Share on other sites More sharing options...
riddhi Posted April 25, 2007 Author Share Posted April 25, 2007 Here is some of the relevant part of the code :- I have used the $productId[] array in my budget.php script to hold the product details. and using the following code:- $arraySer = urlencode(serialize($productId)); print '<input type="hidden" name="productids" value="' . $arraySer . '" />'; to serialise the array. when the user clicks on the add to cart button it calls add_cart.php which then uses the productId array to add the product to the cart:- extract($_GET); extract($_POST); $arraySer = (isset($_POST['productids']))?$_POST['productids']:$_GET['productids']; $arraySerDec = urldecode($arraySer); $array = unserialize($arraySerDec); print_r($array, true); please replace print_r with the for loop traversing the individual array elements. The error it is showing is as follows:- Notice: Undefined index: productids in f:\program files\easyphp1-8\www\plaincart\add_cart.php on line 15 here line 15 is this line:- $arraySer = (isset($_POST['productids']))?$_POST['productids']:$_GET['productids']; Please write code to help me out. Link to comment https://forums.phpfreaks.com/topic/48303-passing-array-between-script/#findComment-238177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.