Incorporating PHP information into javascript

Despite my efforts to find a solution on Google, I have decided to seek assistance here since this platform has helped me with previous questions.

Currently, I am developing an HTML5 canvas game using

PHP, MYSQL, Html5, and JavaScript
.

I have already set up MYSQL databases and a PHP page that displays player high-scores and usernames. Now, my challenge is how to display these high-scores inside the canvas once the game ends and save the high score at the end of the game. Although I have researched AJAX on W3SCHOOLS, I'm still uncertain about the codes to integrate into the JavaScript file. Below are snippets of relevant PHP and script codes:

// Save high score in savescore.php file   
            <?php
                   include 'connect.php';

                $user_score = ($_POST['user_score']);
                $user_name = ($_POST['user_name']);

                if(mysql_query("INSERT INTO users VALUES('$user_name','$user_score')"))
                    echo "Score Successfully Saved";
                else
                    echo "Score Saving Failed";
            ?>
  // Index.php snippet
            <link rel="stylesheet" href="css.css">
            </HEAD>
            <body>     
                <div id="menu">  
                    <a class="item" href="/index.php">Home</a>
              <?php 
                    include 'connect.php';
                    session_start();
                    if($_SESSION['signed_in'])  
                {  
                    echo 'Hello ' . $_SESSION['user_name'] . '. Not you? <a href="signout.php">Sign out</a>';  
                    include 'chat.php';
                }  
                else  
                {  
                    echo '<a href="signin.php">Sign in</a> or <a href="signup.php">create an account</a>.';  
                } 
                ?>
                </div>
            <BODY>
            <canvas id="canvasGAMEOVER" width="800" height="599"> </canvas> 
            <script src="game.js"> </script>

// The part within game.js file responsible for saving scores
            var score = 0;
            function drawGAMEOVER() {
                }

While I managed to connect to the server via AJAX by implementing a simple form in index.php:

<form action="savescore.php">
  user_name: <input type="text" name="user_name"><br>
  user_score: <input type="text" name="user_score"><br>
  <input type="submit" value="Submit">
</form>

In this form, I am unsure of how to retrieve the logged-in user's name (displayed on index.php) along with this.score (shown within the JavaScript file). If anyone can guide me on achieving this or suggest a better approach, I would greatly appreciate it. Thank you in advance for any help or response.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered in parsing JSON: abrupt end of data (JavaScript)

I have been working on a few functions that are responsible for parsing JSON data, both external and internal, and displaying it on a local webpage using the localStorage feature. While I have successfully displayed the external JSON data, I am running int ...

Error message: "Unexpected token" encountered while using the three.js GLTFLoader() function

I have a requirement to load a .glb model into three.js by using the GLTFLoader. I was able to successfully create a rotating 3D mesh on a canvas without encountering any errors in the console. However, when I tried to replace it with the .glb model, I enc ...

The continue to receive "negative one" when using the array.find method

I've encountered an issue with my MEAN js application where the find method on an array is not executing the callback function and just returns -1. Interestingly, when I run this in a standalone file of the project, it works fine. However, once I dec ...

Encountered an error while setting up a DataSource: The 'url' attribute was not provided and an embedded datasource could not be configured. SPRING

I've gone through all the similar questions, and each answer suggests specifying a driverClassName, which I have already done. Below is my application.yml file: spring: application: name: cibus-backend datasource: driverClassName: com.mys ...

Utilize a class method within the .map function in ReactJS

In my ReactJS file below: import React, { Component } from "react"; import Topic from "./Topic"; import $ from "jquery"; import { library } from '@fortawesome/fontawesome-svg-core' import { FontAwesomeIcon } from '@fortawesome/react-fontaw ...

Show the chosen value from the dropdown menu on all JSP pages

I have a header.jsp file containing a dropdown box labeled "Role". This header.jsp is designed to be included in all other JSP files using a directive. Once a user logs in, they are directed to a homepage where they must select a value from the dropdown ...

When SVGs are loaded, vue-svg-loader is known to eliminate select <g> tags

I have successfully integrated an external SVG into my Vue application as a Vue Component using the vue-svg-loader. This is the loader configuration I used to ensure that IDs are not removed: { test: /\.svg$/, loader: 'vue-svg-loader', ...

Transitioning between AngularJS applications - encountering issue with refreshing page content

I have two separate angular applications within my project. The first application serves as the login page, while the second one contains all other functionalities. Both apps share a few services, including one that manages the login logic and information. ...

Is it possible to choose multiple tags using getElementsByTagName?

My website includes a javascript snippet that allows visitors to increase the font size of all paragraphs using the following javascript function: function increaseFontSize() { var paragraphs = document.getElementsByTagName('p'); fo ...

websites utilizing ajax technology

Currently working on a jQuery tutorial and I am curious if there are any publicly available websites that allow for Ajax requests to return JSON strings containing simple lists, strings, objects, etc. Does anyone know of any websites like this? ...

The reason behind the creation of .pnp.loader.mjs and .pnp.cjs files by yarn

While working on my React project with Vite, I noticed that when using yarn to start the "live server," two new files are created: .pnp.loader.mjs and .pnp.cjs. Can anyone explain what these files are for? I've never come across them before as I usual ...

Unable to organize list of entities based on numerical values

I am working with an array of objects structured like this: [ { "value": 351.68474, "o_p": [ "$.text" ] }, { "value": 348.0095, "o_p": [ ...

Navigating a JSON object in d3 after it has been loaded

After loading a JSON object using d3.json and assigning it to a global variable, I encountered an issue where the console returned 'undefined' when printing the global variable. However, typing the global variable in the Chrome console returned t ...

Retrieving data using a MySQL query

I currently have a table stored in the database: https://i.sstatic.net/jPdtN.png I am trying to retrieve the row that is common for subcat_id=1 and subcat_id=8, meaning the row for client_id=2. Could someone provide me with the query that achieves this? ...

Update the content within the Wicket AjaxLink

I recently implemented a new AjaxLink within my .java file. add(new AjaxLink("link"){ ...

What is the best way to send information using AJAX when combining a form submission with a global variable?

I'm currently working on a project where I need to upload a file to my server and save the filename to my database. Although I've successfully saved it to the database, I also want to save the username along with the filename. The issue is that I ...

Encountering an issue when implementing a conditional check within the .map() function utilizing ES6 syntax

I'm struggling to assign only the object that contains an iban value to the list in the code snippet below. I haven't been able to fix this issue on my own and would appreciate some assistance. This.ibanList = this.data.map( (value, in ...

Ways to display JSON data in Angular 2

My goal is to display a list of JSON data, but I keep encountering an error message ERROR TypeError: Cannot read property 'title' of undefined. Interestingly, the console log shows that the JSON data is being printed. mydata.service.ts import { ...

Can the Route be modified in Next.js?

I have developed search functionality that navigates to "/search/xxxx", but it is located in the header. Every time I perform a search, the route gets repeated and becomes "/search/search/xxx". Could you suggest any way other than usin ...

Vite is turning a blind eye to compiler errors until the specific file is executed during runtime

There's an odd issue with Vite that allows me to log in to my app and use it freely, until I click on a page that triggers a compilation error. Once that happens, the page won't render and the error is displayed in the console. Strangely enough, ...