Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  177] [ 1]  / answers: 1 / hits: 12026  / 10 Years ago, sat, march 1, 2014, 12:00:00

Long time stackoverflow user but first time poster! I recently made a very simple little browser based game, and when I say simple I cannot stress this enough.



Anyway, the entire game logic is in one little javascript file. In that file I have a variable called value, and it contains the players score.



The user at any given time during gameplay is allowed to click Submit, and this then uses a php script to access and write their username and hopefully soon their score to my database.



The problem I am having and I know quite a few people new to Ajax have had is, how do I send the value of the javascript variable value to my php page which handles sending the variables to my database?



I have read quite a bit on this and can't seem to get it working. So starting from scratch, assume I have a functioning javascript file with the value variable containing the score. How do I send the score to the php file using ajax? Where would I place the Ajax code?



Thank you stackoverflow! Also I already know that javascript resides on the client side and php runs on the server side, I just would like to know how to properly send the score to the php file, that is all! Thanks again stackoverflow!



EDIT: Because I seem to be getting down voted this is what I have but it does not work:



My Ajax:



function ajaxCall() {alert(To AJAX: the value is:  + value);

$.ajax
( {
type: POST,
url: handler.php,
data: {'value' : value},
success: function(response)
{ alert(The value was passed!)}
}
);};


ajaxCall();



Part of handler.php:



if(isset($_POST['value']))

{
$value = $_POST['value'].;
var_dump($value);
}

var_dump($_POST['value']);

More From » php

 Answers
7

Simply use this,



your ajax.js should contain this.



$(document).ready(function() {
$(#submitscore).click(function(event){
var user = $('#username').val();
var value = document.getElementById('yourscore').innerHTML;
$.post(
submitscore.php,
{ username: user, score: value }
);
});
});


Your other file should have a button with the id submitscore



Hi, <input type=text id=username size=12><br/>
Your Score is : <span id=yourscore>5000</span><br/>
<button id=submitscore>Submit Score</button>


And in that submit file, use this:



if(isset($_REQUEST['score'])||isset($_REQUEST['username']))
{
$username = $_REQUEST['username'];
$value = $_REQUEST['score'];
var_dump($value);
//Your Insertion/Updation Databse Script
}

[#47257] Friday, February 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;