-1

Ok, so let me first explain a couple of things before we get to the point. I'm creating a basic game where you got leaderboards and high scores. When you lose and the game is over, an input field will appear. And here you can type in your name and submit your high score.

And now my point is: How can I get the PHP-code to take the name and the high score that was submitted to my database. The high score is defined inside the Javascript code. Just in case if you wondering, the PHP code and the Javascript code is separated.

Here's how I tried to solve the problem, but it's definitely not working. PHP code only gets the name, and the high score it gets is apparently equal zero.

In the javascript inside a function I tried to use getElementById and then value:

document.getElementById("score").value = duck.highscore ;

<form method="post" action="index.php">
      <input type="hidden" name="submitted" value="true">
      <input type="text" name="score" id="score" value="">  
      <input type="text" id="nameInput" placeholder="your name" maxlength="12"    name="name">
      <input type="submit" id="submitted" value="Submit your highscore">      
</form>




<?php
if (isset($_POST['submitted'])) {   

// connect  to the database      
include('leaderboard.php');

$name = $_POST['name'] ;
$score = $_POST['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" ;


}   // end of main if statement
?>
NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
Will Zhoo
  • 1
  • 5
  • In short, you what the PHP to take data from database? – Carl Binalla Mar 23 '17 at 10:05
  • `duck.highscore` ? what is this `duck`? here – Jai Mar 23 '17 at 10:05
  • 2
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 23 '17 at 10:08
  • function Duck (jump,score, highscore, game, jumph,duck,speed){ this.jump = jump; this.score = score; this.highscore = highscore; this.game = game; this.jumph = jumph; this.duck = duck; this.speed = speed; } – Will Zhoo Mar 23 '17 at 10:41
  • No, I want the php to take data from the input field on the website to the database @Carl – Will Zhoo Mar 23 '17 at 13:32

1 Answers1

1
To fetch the highscore you just posted from the database. 
you must make an ajax call to your server and retrieve it 
as JSON data to dynamically show the inserted values to the
input field. And finally use


<script>
$.getJSON("ajax.php", function(return_data){
$.each(return_data.data, function(key,value){
var value = value.highscore; //returned scored from database
$("#score").attr("value", value);
to append the retrieved value to input value.
);
});
});
</script>



ajax.php

 <?php
 include("connection.php");
 $result=mysqli_query($conn,"select highscore from score");
 while($rec = mysqli_fetch_assoc($result)) 
 {
 $rows[] = $rec;
 }
 $json_row=json_encode(array('data' => $rows));// encode json
 echo $json_row;
 ?>
Wisely D Cruizer
  • 916
  • 9
  • 23