I'm trying to output a JS variable into my PHP-file. And then let the PHP send the variable value into my database. But somehow it doesn't work..
Here I have a form that triggers the JS function: (This file is called index.php)
<form method="post" action="index.php" onsubmit="return phpFunction()" >
<input type="hidden" name="submitted" value="true">
<input type="text" id="nameInput" placeholder="your name" maxlength="12" name="name">
<input type="submit" id="submitted" value="Submit your highscore">
</form>
Here we have the JS function: ( This file is called main.js)
function phpFunction () {
var php = duck.highscore ; // duck.highscore is a number that dynamically changes
window.location.href = "index.php?score=" + php;
};
In the end we got the PHP file that sends the value to my localserver: (This file is inside index.php)
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
$name = $_POST['name'] ;
$score = $_GET['score'] ;
$sqlinsert = "INSERT INTO leaderboard (name, score) VALUES ('$name','$score')" ;
if (!mysqli_query($conn, $sqlinsert)) {
die('error inserting new record') ;
} // end of my nested if statement
$newrecord = "record added" ;
}
?>
When I press the submit button I get a error message like this: