I am sorry if this is a duplicate, but I have searched all over the internet for a solution. I am wondering how I would pass this following code to php
setTimeout(function () {
// did it win??!?!?!
var spin = _this.cache.wheelPos,
degrees = spin % 360,
percent = (degrees / 360) * 100,
segment = Math.ceil((percent / 6)), //divided by number of segments
win = _this.cache.wheelMapping[segment - 1]; //zero based array
console.log('spin = ' + spin);
console.log('degrees = ' + degrees);
console.log('percent = ' + percent);
console.log('segment = ' + segment);
console.log('win = ' + win);
//display dialog with slight delay to realise win or not.
setTimeout(function () {
alert('you won '+win+'!');
}, 700);
I have tried
setTimeout(function () {
$.ajax({
url: 'test.php',
type: 'post',
data: {"win" : +win},
success: function(data) {
}
});
}, 700);
i am testing this by writing it to a text file php file is:
<?php
$_SESSION['test'] = (isset($_POST['win']) ? $_POST['win'] : "";
if (empty($_SESSION['test']){
echo "nothing here";
}else{
$var = $_SESSION['test'];
file_put_contents("test.txt", $var . "
", FILE_APPEND);
exit();
}
?>
which after realising i hadnt put anything in the function i set a alert which shows the following error
<br />
<b>Parse error</b>: syntax error, unexpected ';' in <b>/home/thegrpg/public_html/grpg/test.php</b> on line <b>4</b><br />
Your Data does not seem to be an Array. Try
$.ajax({
url: 'test.php',
type: 'post',
data: [{"win" : +win}]
});
(Converting all my comments into an answer)
Firstly to access sessions we need to call session_start();
at the to of our PHP page. Then there is a missing closing )
for the assignment of $_SESSION['test']
:
<?php
session_start();
$_SESSION['test'] = (isset($_POST['win']) ? $_POST['win'] : "");
/* ^ here */
...
?>
Then in the AJAX call we want to remove the +
from +win
. So change it from:
data: {"win" : +win},
To:
data: {"win" : win},