MuphN Posted September 9, 2014 Share Posted September 9, 2014 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! Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/ Share on other sites More sharing options...
ginerjm Posted September 9, 2014 Share Posted September 9, 2014 How do the js functions get called? Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490497 Share on other sites More sharing options...
MuphN Posted September 9, 2014 Author Share Posted September 9, 2014 On 9/9/2014 at 7:02 PM, ginerjm said: 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490501 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490508 Share on other sites More sharing options...
MuphN Posted September 9, 2014 Author Share Posted September 9, 2014 On 9/9/2014 at 8:03 PM, ginerjm said: 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? Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490513 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490514 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490516 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? Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490522 Share on other sites More sharing options...
MuphN Posted September 10, 2014 Author Share Posted September 10, 2014 On 9/9/2014 at 9:31 PM, ginerjm said: 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? Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490550 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); } }); } ... } Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490573 Share on other sites More sharing options...
MuphN Posted September 10, 2014 Author Share Posted September 10, 2014 On 9/10/2014 at 11:21 AM, Ch0cu3r said: 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490602 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490603 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 Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490607 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. Link to comment https://forums.phpfreaks.com/topic/290951-php-script-kills-js/#findComment-1490609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.