Jump to content


Photo

First time using ajax, having problems.


  • Please log in to reply
1 reply to this topic

#1 smokehut

smokehut
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 18 May 2013 - 08:50 PM

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.



#2 Muddy_Funster

Muddy_Funster
  • Members
  • PipPipPip
  • Advanced Member
  • 3,383 posts

Posted 20 May 2013 - 03:31 PM

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....


Please: "This doesn't work..." is not a question.  We're not the government: we don't have anything to view what your doing on your computer.  Help us help you by asking a good question.

 

"Things needed to ask a "good" question:

  • A description of the context of your issue (Optional but can be a huge help in certain circumstances)
  • What is the actual problem (Mandatory)
  • What does the code actually do (Mandatory)
  • What you think the code should do / What you want the code to do (Mandatory)
  • What things have you tried so far (Optional, but missing it out just wastes your time and ours)
  • The actual code as you are running it - minus any personal information like Database Login Credentials (Mandatory - don't just post pseudo, the vast majority of issues are syntax and not logic)
  • As much info about your development environment as you can give - even if it's just letting us know you are using a hosting provider instead of a local install (Optional, but some questions can not be answered without it.)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users