Jump to content

Please help Simple first time AJAX


nadeemshafi9

Recommended Posts

Hello

i am trying to update some data when a link is clicked using ajax but it dosent update, i have used PHP for quite a while and SQL so im sure theres no probs there. AJAX is a first for me actualy implimenting it myself.

showItem.php
[code]
<?php include("../include/header.php"); ?>
<?php
$sql = "SELECT * FROM items WHERE id='".$_GET["id"]."';";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array($result);

print "".
$row["name"]."<br>".
$row["type"]."<br>".
$row["sub_type"]."<br>".
$row["price"]."<br><br>".
"<a href=showItemImageLarge.php?id=".$_GET["id"]." target=_blank><img src=img.php?id=".$row["id"]." width=50px></a>"."<br><br>".
$row["description"]."<br>"."<br>";
?>
<script type="text/javascript">
function ajaxFunction()

var xmlHttp;
try
{ // Firefox, Opera 8.0+, Safari   
xmlHttp=new XMLHttpRequest();   
}
catch (e)
{ // Internet Explorer   
try
{     
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");     
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;       
}     
}   

xmlHttp.open("GET","recordTimeLeave.php",true);
xmlHttp.send(null);
}
</script>

<a href="javascript:ajaxFunction()">Click</a>

<?php
include("addItemFuzz.php");
?>
<?php include("../include/footer.php"); ?>

[/code]

recordTimeLeave.php
[code]
<?php include("../db_con.php"); ?>
<?php
if (isset($_SESSION["userIsAuthenticated"]))
{
if ($_SESSION["userIsAuthenticated"] == "1")
{
$sql = "UPDATE item_clickstream SET time_left = '22' WHERE id = '1';";

if (mysql_query($sql, $conn)){
print "! item_clickstream RECORDED";
}
else {
print "Error: ".mysql_error($conn);
}
}
}
?>

[/code]

Thanx for any sugestions i have never used ajax before so take this into account

thanx
Link to comment
Share on other sites

i done it

i set a timeout(continuouslyRecordTheTime, 1000) of 1 second to keep calling the script to record the time via <body onLoad="continuouslyRecordTheTime()"> so the function continuouslyRecordTheTime keeps calling the request function which in turn uses the http request object to call the php script to record the time, this all stops when the user navigates away from the page.

showItem.php
[code]
<html>
<body onLoad="continuouslyRecordTheTime()">
<?php include("../include/header.php"); ?>
<?php
//just shows an item
$sql = "SELECT * FROM items WHERE id='".$_GET["id"]."';";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array($result);

print "".
$row["name"]."<br>".
$row["type"]."<br>".
$row["sub_type"]."<br>".
$row["price"]."<br><br>".
"<a href=showItemImageLarge.php?id=".$_GET["id"]." target=_blank><img src=img.php?id=".$row["id"]." width=50px></a>"."<br><br>".
$row["description"]."<br>"."<br>";
?>
<script language="JavaScript" type="text/JavaScript">
function createRequestObject() {

var req;

if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert('There was a problem creating the XMLHttpRequest object');
}

return req;

}

// Make the XMLHttpRequest object
var http = createRequestObject();

function sendRequest() {

// Open PHP script for requests
http.open('get', 'recordtimeleave.php?id=1');
http.send(null);
// CONTINUOUSE UPDATE ajaxTest content *********
setTimeout(continuouslyRecordTheTime,1000);
}

function handleResponse() {

if(http.readyState == 4 && http.status == 200){
// Text returned FROM PHP script
// Do any updates on the page here
var response = http.responseText;
}
}

function continuouslyRecordTheTime() {
sendRequest();
}
</script>

<a href="javascript:continuouslyRecordTheTime()">Click</a><br>

<?php
include("addItemFuzz.php");
?>
<?php include("../include/footer.php"); ?>
</body>
</html>
[/code]

recordTimeLeave.php
[code]
<?php
session_start();

if (isset($_SESSION["userIsAuthenticated"]))
{
if ($_SESSION["userIsAuthenticated"] == "1")
{
mysql_connect("localhost", "user", "pass");
mysql_select_db("coles");
$sql = mysql_query("UPDATE item_clickstream SET time_left = '".time()."' WHERE id = '".$_GET["id"]."';");
}
}
?>

[/code]
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.