I am developing a web site that displays Products, and along with each products there is a Button.There is a <DIV> in my page to display Number of Products Added to the Basket.
Items : 0
When user comes to the page, items will display as 0. if click the Button along with the product items will set to 1. Again user can click product only one time.
Like wise for each and every onclick my Ajax function will fire.
This is how it works:
onclick button Product items will add to MySQL table with session ID
By Calculating Num of Product ID s I will Update it in the DIV.
Again for the for body onload also i am doing the same.for that i will call for onload() function in ajax script. is there any problem in my coding
These things already completed. But there is bug in my application.When i refresh my page all the Added items will set to 0 again.I need to prevent this.
Please help on this.
Do not check the AJAX part just check my PHP code because this is PHP forum But if you can help on that its highly appreciate.
ajax.js
Expand|Select|Wrap|Line Numbers
- var xmlHttp
- var xmlHttp1
- function get_id(p_id)
- {
- xmlHttp=GetXmlHttpObject()
- if (xmlHttp==null)
- {
- alert ("Your Browser is too old please Update It....")
- return
- }
- var url="tocart.php"
- url=url+"?pid="+p_id
- //url=url+"&sid="+Math.random()
- xmlHttp.onreadystatechange=stateChanged
- xmlHttp.open("GET",url,true)
- xmlHttp.send(null)
- }
- function onload(sid)
- {
- xmlHttp=GetXmlHttpObject()
- if (xmlHttp==null)
- {
- alert ("Your Browser is too old please Update It....")
- return
- }
- var url="onload.php?sid="+sid
- xmlHttp.onreadystatechange=stateChanged2
- xmlHttp.open("GET",url,true)
- xmlHttp.send(null)
- }
- function stateChanged()
- {
- if (xmlHttp.readyState==4)
- {
- xmlDoc=xmlHttp.responseXML;
- document.getElementById("sid").innerHTML=xmlDoc.getElementsByTagName("sid")[0].childNodes[0].nodeValue;
- }
- }
- function stateChanged2()
- {
- if (xmlHttp.readyState==4)
- {
- document.getElementById("sid").innerHTML=xmlHttp.responseText;
- }
- }
- function GetXmlHttpObject()
- {
- var objXMLHttp=null
- if (window.XMLHttpRequest)
- {
- objXMLHttp=new XMLHttpRequest()
- }
- else if (window.ActiveXObject)
- {
- objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
- }
- return objXMLHttp
- }
[PHP]<?php
header('Content-Type: text/xml');
//header("Cache-Control: no-cache, must-revalidate");
$pid = $_REQUEST['pid'];
session_start();
$sid=session_id();
require 'dbcon.php';
$chksql = "SELECT p_id FROM cart where ct_session_id= '$sid' AND p_id = '$pid'";
$chkresult=mysql_query($chksql);
if(mysql_num_rows($chkresult) == 0)
{
$sql="INSERT INTO cart (p_id, ct_qty, ct_session_id, ct_date)
VALUES ($pid , 1, '$sid', NOW())";
if (!mysql_query($sql,$con))
{
$sql_count = "SELECT count( p_id ) AS counters FROM `cart`
WHERE ct_session_id = '$sid'";
$result=mysql_query($sql_count);
$row = mysql_fetch_array($result);
$msg = $row['counters'];
}
else
{
$sql_count = "SELECT count( p_id ) AS counters FROM `cart`
WHERE ct_session_id = '$sid'";
$result=mysql_query($sql_count);
$row = mysql_fetch_array($result);
$msg = $row['counters'];
}
}
else
{
$sql_count = "SELECT count( p_id ) AS counters FROM `cart`
WHERE ct_session_id = '$sid'";
$result=mysql_query($sql_count);
$row = mysql_fetch_array($result);
$msg = $row['counters'];
}
echo '<?xml version="1.0" encoding="ISO-8859-1" ?><products>';
echo '<sid>'.$msg. '</sid>';
echo "</products>";
mysql_close($con);
?>[/PHP]
onload.php
[PHP]<?php
$sid = $_REQUEST['sid'];
require 'dbcon.php';
$sql_count = "SELECT count( p_id ) AS counters FROM `cart`
WHERE ct_session_id = '$sid'";
$result=mysql_query($sql_count);
$row = mysql_fetch_array($result);
$msg = $row['counters'];
if($msg==0){
echo "0";
}
else{
echo $msg;
}
mysql_close($con);
?>[/PHP]