0

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:

error 404

Will Zhoo
  • 1
  • 5

3 Answers3

0

You should redirect from your script using header('Location: ' . $your_next_url);.

fedeisas
  • 1,991
  • 2
  • 22
  • 34
  • How do i do that? I'm new to programming so it's a little bit difficult for me to understand what you meant there – Will Zhoo Mar 28 '17 at 21:07
0

You need to pass the query string properly you are missing an = character which is resulting in a URL like: index.php?score1234 which is invalid most likely, ie. "index.php?score" + php;.

In your phpFunction() make it look like this:

function phpFunction () {
    var php = duck.highscore ;   // duck.highscore is a number that dynamically changes 
    window.location.href = "index.php?score=" + php;   
};
segFault
  • 3,887
  • 1
  • 19
  • 31
0

Instead of redirecting to index.php through javascript, try to add score value to the form and submit it.

// HTML Code
<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"> 
  // added hidden input for score.   
  <input type="hidden" id="score" value="" name="score"> 
</form>

// javascript code
function phpFunction () {
  var php = duck.highscore ;   // duck.highscore is a number that dynamically changes
  // update the score field value in the HTML form
  document.getElementById('score').value = php;
  return true;
};

//PHP code

Replace

$score = $_GET['score'];

with

$score = $_POST['score'];
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
  • Clear console and test. Also check by providing static value for score. say document.getElementById('score').value = '12'; and comment var php = duck.highscore ; – Ravinder Reddy Mar 28 '17 at 21:41
  • Done that, I think it's something wrong with the onsubmit. Maybe it's that's the wrong way to trigger the function phpFunction() – Will Zhoo Mar 28 '17 at 21:50