Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:38:40+00:00 2026-05-30T00:38:40+00:00

I’m trying to save a logged in user’s score to my sql database. I

  • 0

I’m trying to save a logged in user’s score to my sql database. I programmed a simple game of pong in HTML5 and javascript. Basically I need to get the score variable from the pong.js file and write that score to the database when the user hits a button on my page. I just simply can’t get it to work. I’ll hit the button as a logged in user and my games score will update to 1 (which is just a test) but i won’t get any type of feedback from my server’s php file.

Here’s my Index.php file:

    <?php 
    session_start(); 
    $_SESSION['score'] = 0;
    ?>
    <html>
    <head>
    <meta name = "description" content = "Josh Siegl's home web server"/>
    <meta name = "keywords" content = "home, web, server, pong, html5, game"/>
    <link rel="stylesheet" type = "text/css" href = "mycss.css" /> 
    <title> JOSH SIEGL HOME WEB SERVER </title>
    </head>
    <body>
    <div align = "center"> 
    <?php if(!isset($_COOKIE['ID_SERVER']))
    {
    echo '<form id = "signIn" action = "getuser.php" method = "post">
    UserName: <input type = "text" name = "username"  />
    Password: <input type = "password" name = "password" />
    <input type = "submit" name = "SIGNIN" value = "SIGN IN"/>  or    
    <a href = "register.html" id = "register" style="text-decoration:none"/> <i>       Register </i></a>
    </form>';
    }
    else 
    {

    ?>

    <p> <?php echo "WELCOME " . $_COOKIE['ID_SERVER'];} ?> <?php         if(isset($_COOKIE['ID_SERVER'])){echo '<a href = "logout.php">  logout</a>';} ?> </p>

    <h1 id = "GAMENAME"> PONG </h1>
    <canvas id="myCanvas" width="800" height="600"></canvas>
    <script type="text/javascript" src = "pong.js" id = "gamescript"></script>
    <div id = "status"></div>
    <script language = "JavaScript" type = "text/javascript">
    function Ajax_Call()
    {
playerscore = 1;
var hr = new XmlHttpRequest(); 

var url = "submitscore.php";
var keys = "score=" + playerscore; 

hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function()
{
    if (hr.readyState == 4 && hr.status == 200)
    {
        var returndata = hr.responseText;
        document.getElementById("status").innerHTML = returndata;
    }
}
hr.open("GET", "submitscore.php?score="+playerscore, true);
hr.send();
document.getElementById("status").innerHTML = "posting..."; 

    }
    </script> 
    <input type = "submit" name = "submitscore" value = "POST SCORE" onclick =    "Ajax_Call();"></input>

    <br /> <p> This is a pong clone, created by Josh Siegl, hosted on a home web server. 
       This game was created in HTML5 and should run on most up to date browsers. </p>
    <br /> <p> I programmed this entire game in javascript and HTML5. The source code is publicly available, right click and hit inspect element </p>

    </div>     
    </body>
    </html>

the submitscore button calls Ajax_Call(), I know that this works because I updated my score variable to show that Ajax_Call is at least executing (the playerscore variable is contained within pong.js, if you need to see that file i’ll post it, although I don’t believe i’ll need to).

Here’s my submitscore php file:

    <?php
    $q=$_GET['score'];
    $con = mysql_connect("localhost", "root", "");
    if (!$con)
    {
die(mysql_error());
    }

    $mydb = mysql_select_db('users', $con);
    if (!$mydb) {
    die ('Can\'t use $con : ' . mysql_error());
}



    $sqlstatement = "UPDATE people
            SET score= $q WHERE username =$_COOKIE['ID_SERVER']";

if (mysql_query($sqlstatement, $con))
{
    echo "your score of: $q is now in the database";
}
else
{
die(mysql_error()); 
}

    mysql_close($con);  

    ?>

(this is just for practice, so don’t go trying to hack my database 😉 ) anyway i’ve been trying a bunch of different things so that’s why the code may look a little bit sloppy, again all I want to do is submit my score variable to the database when the user hits the button. (I’m using firefox to test my site, so at this point to keep things simple, I didn’t throw any checks for other browsers when creating my new xmlhttprequest object if you believe this is the problem let me know)

If you could possibly tell me a direction to go in, or something to try, or at least a website that could help me out. it would be VERY much appreciated. This is my first post so if I did something wrong please let me know. Thank you very much for the help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:38:42+00:00Added an answer on May 30, 2026 at 12:38 am

    An answer the question from the title would be extremely simple: exactly the same way as any other data. PHP knows nothing of AJAX. From the PHP point of view there is no AJAX or browser or bot or whatever but only regular HTTP calls with data. So, PHP have to process that data and store it in the database (or reject), the same way for the all.

    The question body seems more an article rather than a question. Following the stackoverflow habit, I didn’t bother with reading such a novel. Only I can tell that such long posts often inspired by the trivial lack of debugging.

    it seems that you need Firebug as your current debugging tool. A Net tab of this brilliant Fitrefox add-on will show you either HTTP request made my js and a response from PHP. So, it will let you have an idea of what’s going wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.