Jump to content

Php script kills js.


MuphN

Recommended Posts

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 by MuphN
Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.