【发布时间】:2015-02-16 16:14:16
【问题描述】:
我正在尝试设置一个 php 会话 onload
$_SESSION["tusername"] = $_POST['ttuser'];
但是我的值是在$(function() {上的 jQuery 中设置的
$('#ttuser').val(tusername);
加载页面时,未设置会话,因为我相信会话是在 jQuery 设置文本框值之前设置的。我尝试使用 ajax 将值发布到页面,但它没有检索到它。
我现在设置会话的唯一方法是单击按钮,方式完全相同。
有什么想法吗?
提前致谢
编辑 这是开始时的功能,根据要求设置了所有内容
$(function() {
// Initialize. If we are already logged in, there is no
// need for the connect button
Twitch.init({clientId: CLIENT_ID}, function(error, status) {
if (status.authenticated) {
// we're logged in :)
$('.authenticatedd').removeClass('hidden');
} else {
$('.welcometitle').html('<strong>Not yet connected with Twitch?</strong>');
// Show the twitch connect button
$('.authenticate').removeClass('hidden');
}
});
var token = Twitch.getToken();
$('.twitch-connect').click(function() {
Twitch.login({
scope: ['user_read', 'channel_read', 'channel_editor', 'channel_commercial', 'user_subscriptions', 'channel_check_subscription']
});
})
Twitch.api({method: 'channel'}, function(error, channel) {
$('#streamkey').text(channel.stream_key);
});
Twitch.api({method: 'user'}, function(error, user) {
var tusername = user.display_name;
var tlogo = user.logo;
$('#twitchname').text(tusername);
$('#ttuser').val(tusername);
$.get("setsession.php?ttuser="+tusername, function(){
});
console.log(tlogo);
if (tlogo != null)
$('#twitchlogo').attr('src', tlogo);
$.cookie('logo', tlogo, { expires: 14, path: '/'});
$('.sidename').html('<strong>' + tusername + '</strong> Logged in');
var currentdate = new Date();
var datetime = currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var items = [];
items.push('<li><div class="xe-comment-entry"><div class="xe-comment"><a href="#" class="xe-user-name"><span class="label label-success">Status</span></a><p><span class="label label-danger">' +datetime+ '</span> Logged in</p></div></div></li>')
$('#eventlog').prepend( items.join('') );
});
});
【问题讨论】:
-
您能否再澄清一下。至于你想达到什么,你尝试了什么,有什么问题。
-
我们没有足够的代码来做出合理的猜测。
-
基本上,我有一个名为“status.php”的页面,在登录时,页面加载时一个变量被设置为 div 标签值。我正在尝试将会话变量设置为该值。但是,我认为在将文本值设置为 div 标记之前将会话设置为 null。
-
从哪里来(一个变量被设置为一个div标签值)这个变量来了?
-
它来自这里 -> Twitch.api({method: 'user'}, function(error, user) { var tusername = user.display_name; var tlogo = user.logo; $( '#twitchname').text(tusername); $('#ttuser').val(tusername); ....
标签: javascript php jquery ajax session