如何使用jquery中的不同事件更改php的会话值 $(“#家族”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; }); $(“#friends”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; }); $(“#工作”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; }); $(“#其他”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; });
我想用jquery中的各种dblclick事件更改会话值。如何使用jquery中的不同事件更改php的会话值 $(“#家族”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; }); $(“#friends”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; }); $(“#工作”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; }); $(“#其他”).dblclick(函数(){ location.href=“index.php/welcome/home\u images/”; });,php,jquery,Php,Jquery,我想用jquery中的各种dblclick事件更改会话值。 但是它正在存储最后一个值ie=“others”,即使我点击了其他id。您需要使用ajax来实现每个事件的存储会话。在会话中重复其他值的原因这是与此相关的最后一次分配 不能从JavaScript输出PHP代码。使用该代码段,您的浏览器将看到以下内容: $("#family").dblclick(function(){ <?php $_SESSION["album_name"] = "family"; ?> location
但是它正在存储最后一个值ie=“others”,即使我点击了其他id。您需要使用ajax来实现每个事件的存储会话。在会话中重复
其他
值的原因这是与此相关的最后一次分配 不能从JavaScript输出PHP代码。使用该代码段,您的浏览器将看到以下内容:
$("#family").dblclick(function(){
<?php $_SESSION["album_name"] = "family"; ?>
location.href = "<?php echo base_url(); ?>index.php/welcome/home_images/";
});
$("#friends").dblclick(function(){
<?php $_SESSION["album_name"] = "friends"; ?>
location.href = "<?php echo base_url(); ?>index.php/welcome/home_images/";
});
$("#work").dblclick(function(){
<?php $_SESSION["album_name"] = "work"; ?>
location.href = "<?php echo base_url(); ?>index.php/welcome/home_images/";
});
$("#others").dblclick(function(){
<?php $_SESSION["album_name"] = "others"; ?>
location.href = "<?php echo base_url(); ?>index.php/welcome/home_images/";
});
要执行您正试图执行的操作,您需要对服务器进行AJAX调用:
// Assuming $foo is 'bar', will output var foo = 'bar';
var foo = '<?php echo $_SESSION['foo']; ?>';
$("#family").dblclick(function(){
$.ajax({
url: 'http://yourdomain.com/album_name.php',
type: 'post',
data: {albumName: 'family'},
success: function(response) {
// this function will be executed when the server responds successfully to the AJAX call
// response will have whatever you send from the server.
// In this example: 'ok'
alert(response);
}
});
});
然后,在服务器中:
// Assuming $foo is 'bar', will output var foo = 'bar';
var foo = '<?php echo $_SESSION['foo']; ?>';
$("#family").dblclick(function(){
$.ajax({
url: 'http://yourdomain.com/album_name.php',
type: 'post',
data: {albumName: 'family'},
success: function(response) {
// this function will be executed when the server responds successfully to the AJAX call
// response will have whatever you send from the server.
// In this example: 'ok'
alert(response);
}
});
});
唯一可以更改
$\u会话
变量的是服务器。因此,您需要向服务器上的PHP脚本发送请求,并让该脚本更新变量。您不能使用JavaScript在客户端执行此操作
您可以将album_name变量添加到要重定向到的URL的查询字符串中,并让index.php查找该变量并在执行脚本的其余部分之前更新会话值
// album_name.php
$whitelist = array('family', 'friends', 'work', 'others');
if (isset($_POST['albumName'] && in_array($_POST['albumName'], $whitelist))
{
$_SESSION['album_name'] = $_POST['albumName'];
echo 'ok';
}
然后在index.php开头的某个地方
...?album_name=family
确保在调用
session\u start()
或$\u session
变量后出现此错误。它不是这样工作的。了解服务器端语言和客户端语言之间的差异。在本例中,您需要使用AJAX。我建议您学习javascript和php之间的区别。一旦你掌握了一些基本知识,就回来吧。