Jump to content


Photo

First time using ajax, having problems.


  • Please log in to reply
1 reply to this topic

#1 smokehut

smokehut

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 18 May 2013 - 03: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

    Advanced Member

  • Members
  • PipPipPip
  • 2,993 posts

Posted 20 May 2013 - 10:31 AM

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


A simple password hash :

function makePass($word=''){
  $dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
  $dbPass = crypt($word, $dbSalt);
 return substr($dbPass,12);
}



My SQL/PHP Blog




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com