Jump to content

Session creating problem in add to cart script


ayilas

Recommended Posts

Hai every one,

                  I have been doing a jewellery site more over new to php where customers can do shopping online.So, the problem here is with session array variable. It has been a headache for me right from 1 week.

when the customer completes his shopping and went for check out form to fill up his details the session varaible which i used for keeping the shopping items will display all the items.But here the problem comes when the customer moves again for shopping or refreshes the page all the items in  the session variable are not displayed.the code is here it follows.....

first the inner form which is inside the mod---folder named enquiry_form.php...........

 

 

<table width="100%" border="1" align="center" cellpadding="5" cellspacing="5">

  <tr>

    <td valign="top"><table border="0" align="center" cellpadding="0" cellspacing="0">

      <tr>

        <td><br>

          <br>

          <table width="100%"  border="0" cellspacing="0" cellpadding="0">

            <tr>

              <td height="370" valign="top" >

  <table width="80%"  border="0" cellpadding="3" cellspacing="1">

      <form name="frmenqry" onSubmit="return validate()" action="enquiry_form.php" method="post">

                  <tr>

                    <td colspan="3" align="right" >* Fields are mandatory </td>

                  </tr>

                  <tr >

                    <td width="30%" >Name*</td>

                    <td width="2%">:</td>

                    <td width="68%"><input  name="name" type=text maxlength=64>

                    </td>

                  </tr>

                  <tr >

                    <td >Address</td>

                    <td>:</td>

                    <td><textarea  name="address" cols=25 rows=4 wrapping=physical></textarea></td>

                  </tr>

                  <tr>

                    <td> City </td>

                    <td>:</td>

                    <td><input name="city" type=text  id="city" maxlength=32></td>

                  </tr>

                  <tr>

                    <td> Country</td>

                    <td>:</td>

                    <td><input name="country" type=text  id="country" maxlength=32></td>

                  </tr>

                  <tr>

                    <td> Telephone*</td>

                    <td>:</td>

                    <td><input  name="phone" type=text maxlength=32></td>

                  </tr>

                  <tr>

                    <td> E Mail * </td>

                    <td>:</td>

                    <td><input name="email" type=text  id="email" maxlength=32></td>

                  </tr>

                  <tr >

                    <td><span ><span >Products Interested</span></span></td>

                    <td  valign="top">:</td>

                    <td>

<?php

if(!empty($_SESSION['items']) && is_array($_SESSION['items']))

{

$pieces = $_SESSION['items'];

print_r($pieces);

//echo @$_GET['id']."<br>";

foreach($pieces as $k => $items)

{

echo  $pieces[$k];

  ?>

<a href="enquiry_form.php?action=delete&id=<?php echo $k;?>" style="color:#CCCCCC;" >Remove</a>

<br>

<?php

}

}

else

echo "No items selected";

?>

</td>

  </tr>

                  <tr >

                    <td> Query</td>

                    <td>:</td>

                    <td><textarea name="query" cols=25 rows=4  id="query" wrapping=physical></textarea></td>

                  </tr>

                  <tr >

                    <td colspan="3" align="center"><input id="Submit" name="Submit" type="submit" onClick="<?php

if(!isset($items))

{

?>

this.disabled=true;

return true;

<?php

}

?>"  value="Submit">

  <input name="Submit2" type="reset"  value="Reset">

                    </td>

                  </tr>

                </form>

              </table></td>

            </tr>

          </table></td>

      </tr>

    </table>

    </td>

  </tr>

</table>

 

the outside folder which will be having all the include files for check out form

named enquiry_form.php is as follows

 

<?php

session_start();

include_once('inc/application.php');

if((@$_GET['action'] == 'delete')  &&  (is_numeric(@$_GET['id'])))

{

$ary = $_SESSION['items'];

echo $key_to_be_deleted = $_GET['id'];

if (is_array($ary) || is_object($ary))

if(array_key_exists($key_to_be_deleted,$ary))

    $_SESSION['items'] = array_delete($ary,$key_to_be_deleted);

print_r($_SESSION['items']);

}

 

if(!empty($_POST))

{

 

extract($_POST);

$name = addslashes($name);

$address = addslashes($address);

$city = addslashes($city);

$country = addslashes($country);

$phone = addslashes($phone);

$email = addslashes($email);

$subject = "An Order";

$query = addslashes($query);

$message = '';

$headers = "From: $email \r\n" .

    "Reply-To: $email" . "\r\n" .

    'X-Mailer: PHP/' . phpversion();

$mime_boundary = "----MSA Shipping----".md5(time());

$headers .= "MIME-Version: 1.0\n";

$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";

 

$message .= "--$mime_boundary\n";

$message .= "Content-Type: text/html; charset=UTF-8\n";

$message .= "Content-Transfer-Encoding: 8bit\n\n";

 

$data = '';

$total ='';

if(isset($_SESSION))

{ count($_SESSION['items']);

foreach($_SESSION['items'] as $k => $val)

{

$data.= $val."<br>";

$query = $db->query("select *  from product_items where code = '".$val."'");

$res = $db->fetchArray($query);

$total += $res['price'];

    $query = "insert into sold_items(product_code,product_price,date)values('".$val."','".$res['price']."', now())";

$sql = $db->query($query);

}

 

$message .= "<html>\n";

$message .= "<body >\n

<table width=\"800\" height=\"159\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >

<tr>

<td>

Name: $name

</td>

</tr>

<tr>

<td>

Address : $address

</td>

</tr>

<tr>

<td>

City  : $city

</td>

</tr>

<tr>

<td>

Country : $country

</td>

</tr>

<tr>

<td>

Phone Number : $phone

</td>

</tr>

<tr>

<td>

Shopping Items :$data

</td>

</tr>

<tr>

<td>

Total amount : $total

<tr>

<td>

Email Id : $email

</td>

</tr>

</table>

</body>\n

</html>\n";

$message .= "--$mime_boundary--\n\n";

$mail = mail("[email protected],$email",$subject, $message,$headers);

 

unset($_SESSION['items']);

}

$incFILE = 'mod/enquiry_form.php';

}

else

{

$incFILE = 'mod/enquiry_form.php';

}

$left_bar = 'mod/left_menu.php';

include('header.php');

?>

<?php

function array_delete($ary,$key_to_be_deleted)

{

$new = array();

if(!array_key_exists($key_to_be_deleted,$ary)) {

return;

}

if (is_array($ary) || is_object($ary))

foreach($ary as $key => $value) {

if($key != $key_to_be_deleted) {

$new[$key] = $value;

}

}

return $new;

}

?>

 

 

So, kindly help me out to get out of this issue as early as possible.

Any help will be greatly appreciated.......

thanx in advance....................

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.