Jump to content

Passing array between script


riddhi

Recommended Posts

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

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

 

<?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);
}
?>

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()

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.