[英]Pass value from jquery to php
很抱歉,如果这是重复的,但是我已经在整个互联网上搜索了解决方案。 我想知道如何将以下代码传递给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);
我努力了
setTimeout(function () {
$.ajax({
url: 'test.php',
type: 'post',
data: {"win" : +win},
success: function(data) {
}
});
}, 700);
我正在通过将其写入文本文件php文件进行测试:
<?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 . "\n", FILE_APPEND);
exit();
}
?>
意识到我没有在功能中放任何东西后,我设置了一个警报,显示以下错误
<br />
<b>Parse error</b>: syntax error, unexpected ';' in <b>/home/thegrpg/public_html/grpg/test.php</b> on line <b>4</b><br />
您的数据似乎不是数组。 尝试
$.ajax({
url: 'test.php',
type: 'post',
data: [{"win" : +win}]
});
(将我所有的评论转换成答案)
首先,要访问会话,我们需要调用session_start();
在我们的PHP页面的上。 再有就是缺少一个右)
为分配$_SESSION['test']
<?php
session_start();
$_SESSION['test'] = (isset($_POST['win']) ? $_POST['win'] : "");
/* ^ here */
...
?>
然后在AJAX调用,我们要删除+
从+win
。 所以改变它:
data: {"win" : +win},
至:
data: {"win" : win},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.