-2

My web page

I am trying to make a highscore list. i have been seaching everywhere for an answer to this. but I can't seem to find an answer that works. yesterday I made it work yesterday. not sure which code i used think it was something like

window.location = "hihgscore_code.php=" + score

but this make it go to a new web page. i would like it to stay on my web page. then i know i have to use ajax. so i have tried post. and ajax function with the jquery but now where i want the score to stand it only becomes blank. I see no point adding my code here since it has becomed a mess after all my atempts and i am not sure what it says anymore :S

My goal is to send the score from my game made with javascript to php so that it gets the highscore list from the database and then check if the score is high enough to get on the list. then I will use <form> and <input> s to add name to the list. but for that to happen i need to get my javascript variable score into php variable :S

I hope you understand what i am trying to do if not ask. and I will try explaining it better!

i have tried window.location = "hihgscore_code.php=" + score it works but i get rerouted to http://mcclane654-productions.co.nf/game/highscore_code.php?score=7. i also most recently tried

 $.ajax({
                    type: "POST",
                    url: 'highscore_code.php',
   data: "score=" + killed})

and then used

$score = $_POST['score'];

in php to get it.

i did try a version of post yesterday in my javascript and same code in php. also tried with _GET in my php code -.-

  • You need to use `ajax` for that! – Krish R Dec 23 '13 at 14:15
  • ...What have you tried so far? Please post your code. – C_B Dec 23 '13 at 14:15
  • 1
    No, don't post your code if you're saying it's a mess. First, clean it up to work it as a working example (or in your case, not working, but still narrowed down to your problem), then come back and post some code or jsFiddle link. – Laurent S. Dec 23 '13 at 14:17
  • This `window.location = "highscore_code.php=" + score` should probably be `window.location = "highscore_code.php?score=" + score`, then in your PHP code `$score = isset($_GET['score']) ? $_GET['score'] : '';` – stealthyninja Dec 23 '13 at 14:19
  • wait a minute then i will add some code i have tried – Mcclane654 Dec 23 '13 at 14:20
  • now i have added some of the codes i have tried – Mcclane654 Dec 23 '13 at 14:24
  • That has been handled a dozen times here, use the search feature please. – pdu Dec 23 '13 at 14:25
  • pduersteler, thank you for your unessesary comment. if you could learn to read you may find out that i have been searching for an answer to this for 2 Days and still haven't been able to make it work!!!! – Mcclane654 Dec 23 '13 at 14:45

2 Answers2

0

Check this out:

$.ajax({
  type: "POST",
  url: "highscore_code.php",
  data: { score: killed}
})
  .done(function( returned_data ) {

});

Variable returned_data will contain PHP output, for easy communication you should use JSON - in your PHP code echo json_encode (your data - eg. array) function, then use JSON.parse on returned_data so you will get again array of objects/object with easy access to all fields.

Andy
  • 1,035
  • 1
  • 12
  • 28
  • $.ajax({ type: "POST", url: "highscore_code.php", data: { score: killed} }) .done(function( returned_data ) { }); I copied and pasted this to my javascript page. I also made a variable in my php equal to json_encode(score); but I am not sure where to place JSON.parse because it is not readable to php and is the parameter supposed to be returned_data or something else. I have been reading on JSON now but on w3school they only had info about JSON and how to use it with javascript :/ – Mcclane654 Dec 23 '13 at 15:24
  • after some more research i belive you misunderstod my problem. i don't want to return any data to they javascript! after you finish the game the php code prints out the highscore list. but above the list i want the score to be printed. so all i need is to send the score to the php code and make it print it! it would be a lot easier if you could just take a look at the my page. but you don't want to thats fine. but can someone please help me :S? – Mcclane654 Dec 27 '13 at 22:43
0

Since no one wanted to help I found the answer my self. so if anyone else is wondering how to do this use this script:

javascript:

var htmlrequest;
if (window.XMLHttpRequest) {
htmlrequest = new XMLHttpRequest();
} else {
hmtlrequest = new ActiveXObject("Microsoft.XMLHTTP");}
htmlrequest.onreadystatechange = function() {
if (htmlrequest.readyState == 4 && htmlrequest.status == 200) {
  document.getElementById("game").innerHTML = htmlrequest.responseText;}}
htmlrequest.open("POST", "highscore_code.php", true);
htmlrequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
htmlrequest.send("score=" + killed);}

and to get it in php use

$_POST['score']
  • Your code does the same what my code does... My fault I forget to mention mine needs http://jquery.com/ library to work. But thanks to this the code is shorter. – Andy Dec 29 '13 at 16:59