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

 

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.