0

I have a script that will get the users current time. Then I pass this variable to PHP to display content on the site. When I first load the page the PHP does not display the query results until the page is reloaded. How can I get it to appear when a user first accesses the web page? I am using wordpress.

<script type="text/javascript">


<script src="jquery-3.2.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://www.developwww.com/js-cookie-1.5.1/src/js.cookie.js"></script>
<script>
function client_date() {
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var x = month[d.getMonth()];
var y=d.getDate();
var z=d.getFullYear();
var s=x+" "+y+","+z;
    return s ;
}
var day = Math.round((new Date().setHours(23) - new Date(new 
Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
$.cookie("date", client_date());
$.cookie("day", day);
</script>

Here is the PHP

 $dayYear=$_COOKIE["day"]; 
$date=$_COOKIE["date"];
echo  $date ."<br> " ;

$query = " select C.book,B.fchapter, B.lchapter, B.fverse ,B.lverse 
FROM readingplan B, biblebooks C WHERE B.id = $dayYear and 
C.id=B.fbook";

And then I have it display the results.

6
  • your problem is not the javascript not beeing executed, but your attempt to pass values via cookies. And your way to find out the users current date (=timezone) seems quite too complicated... Commented Jun 30, 2017 at 17:03
  • what you might want to do is an ajax call with those to values (day & date) to get result from db Commented Jun 30, 2017 at 17:03
  • Why do you working with the date on client-side(javascript) then passing to server-side(php) ? Why don't you do everything on server-side ? Commented Jun 30, 2017 at 17:13
  • When I get the date from the server-side it uses UTC. I need the date and time to be the same as the users. I worked with my provider to fix this on the server side. Due to the wordpress theme. it does not work that way. Commented Jun 30, 2017 at 17:20
  • Well I see two ways. Using ajax or you can uses a require. First require the cookie setter then you require the php that uses the cookies. Make sense to you ? Commented Jun 30, 2017 at 17:31

1 Answer 1

3

The reason for your problem:

You load the page first time (no cookies are stored yet) so your logic doesn't work and after the user receives the page, the cookie is saved so the second request will find this cookie and let your logic work.

My Recommendation:

I see that you do not do any client side stuff to be restricted to Javascript. My recommendation is to calc whatever the date you need on the server with the PHP

Therefore, no need to send anything to server neither by cookie nor anything else. Just prepare the date and do your query then send the response which guarantees to work with the first request as you want

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.