MuphN Posted September 9, 2014 Share Posted September 9, 2014 (edited) Hello, so I have trubble with one thing, I created a countdown with cookies and javascript, and I want to do something when it counts to 0. So I tryed adding a bit php to js. Hares what I got. My main.php page (lets say its main.php). <?php include_once 'includes/uhp.php'; ?> <script type="text/javascript"> function setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime()+(exdays*24*60*60*1000)); var expires = "expires="+d.toGMTString(); document.cookie = cname + "=" + cvalue + "; " + expires; } function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i<ca.length; i++) { var c = ca[i].trim(); if (c.indexOf(name)==0) return c.substring(name.length,c.length); } return ""; } //check existing cookie cook=getCookie("my_cookie"); if(cook==""){ //cookie not found, so set seconds=60 var seconds = 60; }else{ seconds = cook; console.log(cook); } function secondPassed() { var minutes = Math.round((seconds - 30)/60); var remainingSeconds = seconds % 60; if (remainingSeconds < 10) { remainingSeconds = "0" + remainingSeconds; } //store seconds to cookie setCookie("my_cookie",seconds,5); //here 5 is expiry days document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds; if (seconds == 0) { clearInterval(countdownTimer); document.getElementById('countdown').innerHTML = "<?php uhpn($mysqli); ?>"; setCookie('my_cookie', '', -1); } else { seconds--; } } var countdownTimer = setInterval(secondPassed, 1000); </script> <span id="countdown" class="timer"> </span> And thats my uhp.php that I included at the top. <?php include_once 'db_connect.php'; include_once 'functions.php'; sec_session_start(); function uhpn($mysqli) { if ($stmt = $mysqli->prepare("UPDATE members SET count = count+1 WHERE id = ? LIMIT 1")) { $user_id = $_SESSION['user_id']; $stmt->bind_param('i', $user_id); // check if the query did execute and that it affected a row if ($stmt->execute() && $mysqli->affected_rows > 0) { exit; } } return false; } So I tryed setting that it would only say like TEST when the counter hits 0, it works. Everything is fine, but whenever I add the php part to the js timer end. It runs the script but it runs allways when u refresh it, but it doesnt trigger when its 00:00, and when refresh it kills the script. So I think its my php foult for some reason, it only runs the php, it doesnt cares about js part. Any help, thank you in advanced! Edited September 9, 2014 by MuphN Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 9, 2014 Share Posted September 9, 2014 How do the js functions get called? Quote Link to comment Share on other sites More sharing options...
MuphN Posted September 9, 2014 Author Share Posted September 9, 2014 How do the js functions get called? by the <span id="countdown" class="timer"></span> - in other words it shows the outcome in this span. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 9, 2014 Share Posted September 9, 2014 You can't call PHP from there. You're at the client - php is on the server. Besides no matter what you put into the span tag how does it get triggered? You use a timer (setinterval) but all that does is do a countdown as you want - it doesn't trigger anything. Quote Link to comment Share on other sites More sharing options...
MuphN Posted September 9, 2014 Author Share Posted September 9, 2014 You can't call PHP from there. You're at the client - php is on the server. Besides no matter what you put into the span tag how does it get triggered? You use a timer (setinterval) but all that does is do a countdown as you want - it doesn't trigger anything. True, how would you think I should do? I mean I though countdown with cookies will help, wouldnt it be better to make a timer with server side time so for example if timer started it gets the server time, adds +60 seconds to it and makes it the finishing time, how would I do that? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 9, 2014 Share Posted September 9, 2014 When your js gets to 0 then let js trigger the action. It could be a submit of a form, or it could be an ajax call. Quote Link to comment Share on other sites More sharing options...
MuphN Posted September 9, 2014 Author Share Posted September 9, 2014 How can I make that js would trigger the script? The script is including the database, but js cant do that. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 9, 2014 Share Posted September 9, 2014 Then that would entail an ajax call to a php script, wouldn't it? Quote Link to comment Share on other sites More sharing options...
MuphN Posted September 10, 2014 Author Share Posted September 10, 2014 Then that would entail an ajax call to a php script, wouldn't it? I do realise that, but I dont really imagine how to play with ajax like that. Could you help me? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 10, 2014 Share Posted September 10, 2014 ginerjm is right you need to use ajax in order to call your PHP script when the timer reaches zero. Example using jQuery.ajax method function secondPassed() { ... if (seconds == 0) { clearInterval(countdownTimer); // using jQuery ajax method, send request to uhp.php $.ajax('uhp.php', { success: function(response) { // the response (output) from the script will be added to div#countdown $('#countdown').html(response); } }); } ... } Quote Link to comment Share on other sites More sharing options...
MuphN Posted September 10, 2014 Author Share Posted September 10, 2014 ginerjm is right you need to use ajax in order to call your PHP script when the timer reaches zero. Example using jQuery.ajax method So. If I would do this. function secondPassed() { var minutes = Math.round((seconds - 30)/60); var remainingSeconds = seconds % 60; if (remainingSeconds < 10) { remainingSeconds = "0" + remainingSeconds; } //store seconds to cookie setCookie("my_cookie",seconds,5); //here 5 is expiry days document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds; if (seconds == 0) { clearInterval(countdownTimer); document.getElementById('countdown').innerHTML = "T"; $.ajax('includes/uhp.php', { //calls uhp.php? success: function(response) { //What this function does with response? $('#countdown').html("DUNE"); //This writes Dune in html countdown span right? } } setCookie('my_cookie', '', -1); } else { seconds--; } } Could you explain this one? Its not just to get the script working, I just need to understand so I wouldnt bother you next time guys. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 10, 2014 Share Posted September 10, 2014 Why don't you try doing some research on your own? That's how one learns. Quote Link to comment Share on other sites More sharing options...
MuphN Posted September 10, 2014 Author Share Posted September 10, 2014 ginerjm yep you are right. I did some research and now I do understand the script. But not it gets stuck at 00:00 counter doesnt go. Just shows 00.00 Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 10, 2014 Share Posted September 10, 2014 Open your developer console in the browser and check the headers and return values. There are a lot of possibilities for why the counter script isn't firing - it'll be easier to debug once you know how far the script is executing. 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.