Jump to content

Recommended Posts

What I'm trying to do in a nutshell is create a full 5 step interaction between a user and a "host" on my site, without the need for one refresh, as I designed my website for fast-linking with #tag for selecting pages. so if a refresh occurs it's back to the home page :P.

 

Basically, what I've got.

 

ajax.js

// Customise those settings

var seconds = 5;
var divid = "timediv";
var url = "boo.php";

////////////////////////////////
//
// Refreshing the DIV
//
////////////////////////////////

function refreshdiv(){

// The XMLHttpRequest object

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

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}

Then, boo.php

<?
/*$query = "SELECT * FROM deposits WHERE user = ? AND completed = 0";
$stmt = $db->prepare($query);
$stmt->bindValue(1, $user_data['username']);
$stmt->execute();

$fetch = $stmt->fetch(PDO::FETCH_ASSOC);*/


// Format time output

$str = "It is %a on %b %d, %Y, %X - Time zone: %Z";

// Echo results

echo(gmstrftime($str,time()));

?>


^ Above is what I'm trying to achieve, but I cannot even get it to echo the time in my other php form that requires the script.

<script src="ajax.js"></script>

<strong>This is the current date and time (updates every 5 seconds):</strong>

<script type="text/javascript"><!--
refreshdiv();
// --></script>
<div id="timediv"></div>

Can anyone point a beginner in the right direction?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/278142-first-time-using-ajax-having-problems/
Share on other sites

I'd like to point you to jquery. as a beginner your would be better off starting to learn that than oldschool "raw" javascript - especially for ajax.

 

another thing - you seem to have commented out the part of the script that actually calls the function....

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.