2016-08-05 5 views
0
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); 
    } 
} 

Also, der Titel sagt alles. In diesem Code versuche ich eine JavaScript-Bedingung zu verwenden, um zu bestimmen, wann ich meine Sitzungsvariable erhöhen werde. Wie kann ich das machen? Vielen Dank im Voraus für die Hilfe alle!Inkrementieren einer PHP-Session innerhalb einer JS-Conidtion

Antwort

1

Dies ist auf diese Weise nicht möglich. Wann immer dein Skript bereit ist und laden dann und dort PHP-Code wird ausgeführt, so hängt nicht von Ihrem js Funktionsaufruf oder js Bedingungen. Denken Sie immer daran, PHP ist serverseitige Skriptsprache und JavaScript ist clientseitige Skripterstellung.

Sie können es mit Ajax oder Jquery Post versuchen.

wie auf diese Weise versuchen ..

Wenn Sie nicht jQuery-Bibliothek-Datei hinzugefügt werden, dann können Sie es in Ihrem Header hinzufügen

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

Jetzt Ihr Code:

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']; 

?> 
+0

Ich werde schauen in das Morgen, danke! –

+0

Also, hoffentlich verstehe ich das richtig. Dies bucht nun den Wert der JS-Score-Variable in die Datei serverfile.php, die dann die Sitzung aktualisiert, richtig? Brauche ich nicht auch einen session_start() in der serverfile.php oder nein? –

+0

Ich weiß nicht, Ihr Framework, das Sie verwenden, wenn in jeder Datei Ihres Codes vor dieser Datei enthalten Sitzung gestartet wird gestartet dann keine Notwendigkeit, Sitzung zu starten, wenn nicht, dann sollten Sie Sitzung starten. –

Verwandte Themen