Brandon Chatham
Brandon Chatham

Reputation: 5

Incrementing a php session within a JS conidtion

this.collision = function(p1, p2, p3, p4)
{
    if(this.ballY - this.radius + this.dy <= p1.height && this.ballX + this.dx >= p1.leftPostx - p1.goalPostRadius && this.ballX <= p1.rightPostx + p1.goalPostRadius)
    {
        this.dy = -this.dy;
        this.ballY = p1.height + this.radius;
        count++;
        score++;
        <?php $_SESSION['score'] += 1; ?>
    }
    else if(this.ballY + this.radius + this.dy >= canvas.height - p2.height && this.ballX + this.dx >= p2.leftPostx - p2.goalPostRadius && this.ballX + this.dx <= p2.rightPostx + p2.goalPostRadius)
    {
        this.dy = -this.dy;
        this.ballY = canvas.height - p2.height - this.radius;
        count++;
        score++;
        <?php $_SESSION['score'] += 1; ?>
    }
    else if(this.ballX + this.radius + this.dx >= canvas.width - p3.height && this.ballY + this.dy >= p3.topPosty - p3.goalPostRadius && this.ballY + this.dy <= p3.bottomPosty + p3.goalPostRadius)
    {
        this.dx = -this.dx;
        this.ballX = canvas.width - p3.height - this.radius;
        count++;
        score++;
        <?php $_SESSION['score'] += 1; ?>
    }
    else if(this.ballX - this.radius + this.dx <= p4.height && this.ballY + this.dy >=  p4.topPosty - p4.goalPostRadius && this.ballY + this.dy <= p4.bottomPosty + p4.goalPostRadius)
    {
        this.dx = -this.dx;
        this.ballX = p4.height + this.radius;
        count++;
        score++;
        <?php $_SESSION['score'] += 1; ?>
    }
    else if(this.ballY - this.radius + this.dy < 0 || this.ballY + this.radius + this.dy > canvas.height || this.ballX - this.radius + this.dx < 0 || this.ballX + this.radius + this.dx > canvas.width)
    {
        checkLives(p1, p2, p3, p4, ball1);
    }
}

So, the title says it all. In this code I am trying to use a javascript condition to determine when I will be incrementing my session variable. How can I do this? Thanks in advance for the help everyone!

Upvotes: 0

Views: 41

Answers (1)

Diptesh Atha
Diptesh Atha

Reputation: 911

This is not possible like this way. When ever your script ready and load then and there php code will execute so does not depends on your js function call or js conditions. Always remember php is server side scripting language and JavaScript is client side scripting.

You can try it using ajax or jquery post.

Try like this way..

If you are not added jQuery library file then you can add it in your header

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

Now your code is:

var myObj = new function(){
    this.collision = function(p1, p2, p3, p4)
    {
        if(this.ballY - this.radius + this.dy <= p1.height && this.ballX + this.dx >= p1.leftPostx - p1.goalPostRadius && this.ballX <= p1.rightPostx + p1.goalPostRadius)
        {
            this.dy = -this.dy;
            this.ballY = p1.height + this.radius;
            count++;
            score++;
        }
        else if(this.ballY + this.radius + this.dy >= canvas.height - p2.height && this.ballX + this.dx >= p2.leftPostx - p2.goalPostRadius && this.ballX + this.dx <= p2.rightPostx + p2.goalPostRadius)
        {
            this.dy = -this.dy;
            this.ballY = canvas.height - p2.height - this.radius;
            count++;
            score++;
        }
        else if(this.ballX + this.radius + this.dx >= canvas.width - p3.height && this.ballY + this.dy >= p3.topPosty - p3.goalPostRadius && this.ballY + this.dy <= p3.bottomPosty + p3.goalPostRadius)
        {
            this.dx = -this.dx;
            this.ballX = canvas.width - p3.height - this.radius;
            count++;
            score++;
        }
        else if(this.ballX - this.radius + this.dx <= p4.height && this.ballY + this.dy >=  p4.topPosty - p4.goalPostRadius && this.ballY + this.dy <= p4.bottomPosty + p4.goalPostRadius)
        {
            this.dx = -this.dx;
            this.ballX = p4.height + this.radius;
            count++;
            score++;

        }
        else if(this.ballY - this.radius + this.dy < 0 || this.ballY + this.radius + this.dy > canvas.height || this.ballX - this.radius + this.dx < 0 || this.ballX + this.radius + this.dx > canvas.width)
        {
            checkLives(p1, p2, p3, p4, ball1);
        }

        myObj.updateCore(score);
    }
    this.updateCore = function(addIncrement){
            jQuery.ajax({
              dataType: "json", 
              method:"POST",
              url:PATH_TO_FILE+'/serverfile.php',
              data:{score:addIncrement},
              async:false,
              beforeSend:function(){},
              success:function(data){},
              error:function(request, status, error) {
                alert(request.responseText);
              },
              complete: function(){}
            });  
        }   
    }

in serverfile.php

<?php 

$_SESSION['score'] = $_POST['score'];

?>

Upvotes: 1

Related Questions