Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  139] [ 1]  / answers: 1 / hits: 16231  / 11 Years ago, mon, november 4, 2013, 12:00:00

I've got some PHP code where it says the time you signed up, and gives an approximation of how long ago it was.
I've been looking into trying to refresh it with javascript, but I'm struggling to find exactly what I'm after, and as my knowledge of it isn't good I'm a bit stuck on getting it working now.



Basically, this is the code I currently have, with the function being the thing displaying the time since singing up



Date registered:  <?PHP echo date('jS F Y', $_SESSION['User_Date']). at .date('G:i:s', $_SESSION['User_Date']);
echo (.time_delta('now',date('F j Y G:i:s', $_SESSION['User_Date'])).); ?>


Which will output something like:
Date registered: 4th November 2013 at 0:57:31 (About 34 minutes ago)



And I've got this for javascript, but I don't know how to make it just update the text and not redirect the page.



 <script>
setInterval(function() {
window.location.href = timeupdate.php;
}, 1000);
</script>


Would there be a simple way to do this, or is it worth avoiding it in terms of cpu usage?


More From » php

 Answers
78

As the comments have suggested AJAX is your answer.



With jQuery an ajax call would look something like this:



$(document).ready(function() {
setInterval(function(){
$.ajax({
url: timeupdate.php,
type: GET,
dataType: html,
success: function(html) {
$(#date_registered_element).html(html);
}
});//end ajax call
},1000);//end setInterval
});//end docReady


This will output the text returned from timeupdate.php to a div with the id of date_registered_element -> change this to the name of the div to which you wish to output the updated text.



In order to pass a session variable to the Ajax function you need to have the server echo out the Session variable before the script runs (i.e. as the page is rendered) something like this:
HTML:



<div id=user_date style=display: none;>
<?php echo $_SESSION[User_Date]; ?>
</div>


Please NOTE: You need to be very careful with this. Anyone who looks at your markup in a browsers View Source or Developer Tools will be able to see the SESSION Variable echod out. You should only do this if the data represents no security risk. In my opinion you would be better passing no GET variables in the Ajax call and just referencing the SESSION variable in the timeupdate.php script. The point of SESSION variables is to provide state-like functionality to HTTP so you should never need to send SESSION variables over a HTTP request.



That said, if you wish to do it the way you currently have it set up then the code is below.



Once you have echoed out the SESSION variable onto the page, you can then collect the value from the id with jQuery and pass it to the Ajax function:



Javascript/jQuery:



var url = timeupdate.php?time= + $(#user_date).text();

//pass the url to the Ajax function in the ajax function like this:
$.ajax({
url: url,
//the rest of your ajax function here
});


if you add jQuery to your page <head></head> tag BEFORE your existing script this should work:



<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js>

[#74533] Friday, November 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;