dezkit Posted September 26, 2008 Share Posted September 26, 2008 i have this php code <?php session_start(); session_register("count"); $sessid = session_id(); if (!isset($_SESSION)) { $_SESSION["count"] = 0; echo "<p>Counter initialized</p>\n"; } else { $_SESSION["count"]++; } echo "<p>The counter is now <b>$_SESSION[count]</b></p>"; echo "<p>please reload this page to increment</p>"; ?> Click <a href="javascript:function?">here</a> to refresh the page. And i was wondering so that if somebody presses "here" the counter adds by one without the browser refreshing, thanks and please post back Quote Link to comment Share on other sites More sharing options...
xtopolis Posted September 26, 2008 Share Posted September 26, 2008 You would need ajax to send the request to the same or a different page, and then update the result on your page. It would be easier to refresh the page, than to use ajax to add to the session counter... Quote Link to comment Share on other sites More sharing options...
dezkit Posted September 26, 2008 Author Share Posted September 26, 2008 can you or somebody give me the ajax code please? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted September 26, 2008 Share Posted September 26, 2008 //CREATES A XMLHttpRequestObject function newXHRO() { try { return new XMLHttpRequest(); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} alert("XMLHttpRequest not supported"); return null; } //Make call (pass the function an id function updateCtr(what) { var x = document.getElementById(what); x.innerHTML = 'Updating...'; var XHRO = new newXHRO(); var url = 'scriptname.php'; // CHANGE THIS TO YOUR URL XHRO.onreadystatechange=function() { if(XHRO.readyState==4 && XHRO.status==200) { x.innerHTML = XHRO.responseText; } } XHRO.open("GET",url,true); XHRO.send(NULL); } Quote Link to comment Share on other sites More sharing options...
dezkit Posted September 26, 2008 Author Share Posted September 26, 2008 Whoa! thats a wall of text Can you put that in my code? Thanks sooo much, im not that professional at ajax/php Quote Link to comment Share on other sites More sharing options...
xtopolis Posted September 26, 2008 Share Posted September 26, 2008 No I won't code it for you :3 You're welcome. (I told you it would be easier to just refresh the page.) Quote Link to comment Share on other sites More sharing options...
dezkit Posted September 26, 2008 Author Share Posted September 26, 2008 what? lol. <?php session_start(); session_register("count"); $sessid = session_id(); ?> <script language="javascript" type="text/javascript"> <!-- function newXHRO() { try { return new XMLHttpRequest(); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} alert("XMLHttpRequest not supported"); return null; } //Make call (pass the function an id function updateCtr(what) { var x = document.getElementById(what); x.innerHTML = 'Updating...'; var XHRO = new newXHRO(); var url = 'test1.php'; // CHANGE THIS TO YOUR URL XHRO.onreadystatechange=function() { if(XHRO.readyState==4 && XHRO.status==200) { x.innerHTML = XHRO.responseText; } } XHRO.open("GET",url,true); XHRO.send(NULL); } //--> </script> <?php if (!isset($_SESSION)) { $_SESSION["count"] = 0; echo "<p>Counter initialized</p>\n"; } else { $_SESSION["count"]++; } echo "<p>The counter is now <b>$_SESSION[count]</b></p>"; echo "<p>please reload this page to increment</p>"; ?> Click <a href="javascript:updateCtr(what)">here</a> to refresh the page. is this corerct? Quote Link to comment Share on other sites More sharing options...
web_loone_08 Posted September 26, 2008 Share Posted September 26, 2008 that is your ajax code - that xtopolis just provided you: //CREATES A XMLHttpRequestObject function newXHRO() { try { return new XMLHttpRequest(); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} alert("XMLHttpRequest not supported"); return null; } //Make call (pass the function an id function updateCtr(what) { var x = document.getElementById(what); x.innerHTML = 'Updating...'; var XHRO = new newXHRO(); var url = 'scriptname.php'; // CHANGE THIS TO YOUR URL XHRO. /> { if(XHRO.readyState==4 && XHRO.status==200) { x.innerHTML = XHRO.responseText; } } XHRO.open("GET",url,true); XHRO.send(NULL); } put your php script in a different page and name it "scriptname.php" (for example purposes only - name it what you want to; just make sure you define the "url" variable in the AJAX script above; with the same name). <?php session_start(); session_register("count"); $sessid = session_id(); if (!isset($_SESSION)) { $_SESSION["count"] = 0; echo "<p>Counter initialized</p>\n"; } else { $_SESSION["count"]++; } echo "<p>The counter is now <b>$_SESSION[count]</b></p>"; echo "<p>please reload this page to increment</p>"; ?> then in your html page; if your calling the function with link - do it something like this: <span id="counter"></span> Click <a href="javascript:updateCtr('counter')">here</a> to refresh the page. Quote Link to comment Share on other sites More sharing options...
dezkit Posted September 26, 2008 Author Share Posted September 26, 2008 i can't get it to work, whatever, i give up, thanks so much guys! im very very appreciated Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.