1
    var hiScore = 0;
    var userip;

    function onGameOver(){
    if (-1 < score && score < 6) { doStuff(); }
    if (5 < score && score < 9) { doStuff2(); }
    if (8 < score && score < 15) { doStuff3(); }
    if (14 < score && score < 21) { doStuff4(); }           
    if (20 < score && score < 27) { doStuff5(); }
    if (26 < score && score < 31) { doStuff6(); }
    if (30 < score && score < 36) { doStuff7(); }
    if (35 < score && score < 51) { doStuff8(); }
    if (50 < score && score < 69) { doStuff9(); }
    PostTo();
    }

    function PostTo() {
    $.ajax({
        url:'Score.php',
        type:'post',
        data:{hiScore:hiScore,
              userip:userip},
        success:function(data){
        alert('Success');
    }
    });
    }

the $.ajax part of the code seems to break the js code. I've no idea why. Maybe I'm using it wrong. here is the php as well.

<?php

$hiScore = $_POST['hiScore'];
$userip = $_POST['userip'];
$file = fopen('file.txt','w+');
fwrite($file, $hiScore.'\t'.$userip);
fclose($file);

?>

Any idea what is going on? The code starts working again perfectly after I remove the post code.

How it breaks: obviously there is more to the code its a game simply put the game doesn't start it just "breaks".

Code edited

It still isn't posting to the file.txt file. Thanks to baao for pointing out mistakes!

3
  • 1
    Explain how it is breaking. Commented Dec 7, 2014 at 2:17
  • seems like I forgot the bracket in my other answer. I've corrected it for you... :) Commented Dec 7, 2014 at 2:19
  • 1
    <?php? is invalid, as is the closing >. In the Javascript, the score variable is never defined. Did you mean hiScore? You should pass things as arguments, instead of using global variables. Global variables are a horrible idea. Commented Dec 7, 2014 at 2:19

1 Answer 1

1
function PostTo() {
$.ajax({
    url:'src/Score.php',
    type:'post',
    data:{hiScore:hiScore,
          userip:userip},
    success:function(data){
    alert(data);
    } // <<<<<<<<<<<<<<<<you have forgotten the closing bracket here 
});
}

it is <?php, not <php? to open php; to close php you need ?> not >

<?php // <<<<< here 

$hiScore = $_POST['hiScore'] ? $_POST['hiScore'] : 'not set';
$userip = $_POST['userip'] ? $_POST['userip'] : 'not set';
$file = fopen('file11111111.txt','a+');
fwrite($file, 'Score: '.$hiScore.' IP is: '.$userip.' '.PHP_EOL);
fclose($file);
echo "I was here!";

?>  // <<<<<< and here

if (-1 < score && score < 6) { doStuff(); }       //  |
if (5 < score && score < 9) { doStuff2(); }       //  |
if (8 < score && score < 15) { doStuff3(); }      //  | 
if (14 < score && score < 21) { doStuff4(); }     //  |     
if (20 < score && score < 27) { doStuff5(); }     //  |||||| should all be hiScore?!?
if (26 < score && score < 31) { doStuff6(); }     //  |
if (30 < score && score < 36) { doStuff7(); }     //  |
if (35 < score && score < 51) { doStuff8(); }     //  |
if (50 < score && score < 69) { doStuff9(); }     //  |
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.