session...

I have a html page (product.html) and php page (session.php)…

i need to pass values from product page to session page… for session creation…

everything works fine…But i need to set session values again for the changes of new qty.

this is my code…

Product.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="15%">&nbsp;</td>
      <td width="15%">Items</td>
      <td width="70%">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><a href="session.php?itemID=1000139&qty=1">1000139</a></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><a href="session.php?itemID=1000140&qty=3">1000140</a></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><a href="session.php?itemID=1000141&qty=2">1000141</a></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

Session.php

<?php

$itemID=$_GET['itemID'];
$qty=$_GET['qty'];

if(!isset($_SESSION['cart'])){
	session_start();
	session_register('cart');
}

$_SESSION['cart'][$itemID]=$qty;

if(isset($_POST['hd']) && $_POST['hd']=='form1'){
	foreach($_POST as $key=>$val){
		echo $key."=======>".$val."<br>";
		$_SESSION['cart'][$itemID]=$val;
	}
	$location="<script>window.location='session.php'</script>";
	echo $location;
	unset($location);
	//header("location:session.php");
}

?>
...........
...........
<?php
		foreach($_SESSION['cart'] as $key=>$val){
		//echo $key."=======>".$val."<br>";
	?>	

    <tr>
      <td>&nbsp;</td>
      <td><?php echo $key?></td>
      <td><input name="<?php echo $key?>" type="text" size="5" value="<?php echo $val?>"/></td>
      <td>10</td>
      <td>&nbsp;</td>
    </tr>
	<?php
		}
	?>
.................
.................
<td><input type="submit" name="Submit" value="Calculate" />
      <input name="hd" type="hidden" id="hd" value="form1" /></td>

the problem is… when i click on Calculate button,
the text box value changes as ‘form1’…

Can anyone help me to figure out this problem???Pls

shouldn’t [php]$_SESSION[‘cart’][$itemID]=$val;[/php](inside the foreach loop) be [php]$_SESSION[‘cart’][$key]=$val;[/php]?

thanks for the reply…

but it doesn’t seems to solve the problem…

one more thing i found:
[php]if(!isset($_SESSION[‘cart’])){
session_start();
session_register(‘cart’);
} [/php]

should be:
[php]session_start();
if(!isset($_SESSION[‘cart’])){
session_register(‘cart’);
} [/php]
could be that $_SESSION[‘cart’] never it set before calling session start, i never tryed.

hope that this makes a difrence

2 more things:
i would use an if for the first part, as u did for the second:
[php]
session_start();
if( isset($_GET[‘itemID’]) && isset($_GET[‘qty’]) ) {
$itemID=$_GET[‘itemID’];
$qty=$_GET[‘qty’];

if(!isset($_SESSION[‘cart’])){
session_register(‘cart’);
}

$_SESSION[‘cart’][$itemID]=$qty;
}
[/php]

and i would modify the foreach so u don’t get a value $_SESSION[‘cart’][‘hd’]=‘form1’;

maybe:
[php]foreach($_POST[‘card’] as $key=>$val){[/php]
and

<td><input name="card[<?php echo $key?>]" type="text" size="5" value="<?php echo $val?>"/></td>
Sponsor our Newsletter | Privacy Policy | Terms of Service