【问题标题】:How to save the value of the selected item of a dropdown list in a session如何在会话中保存下拉列表中所选项目的值
【发布时间】:2016-04-20 16:20:50
【问题描述】:

我想在会话中保存下拉列表中的选定项目。 我的会议呼吁如下:

$('#airport-select').change(function(){

    $.ajax({
        url:"php/createSession.php",
        method:"GET",
        data: { 
            session_name: 'airportid', 
            session_value: $(this).val() 
        }
    })  
    .success(function(data){
        alert('Sucess');
    })
    .error(function(e){
        console.log("Erorr");
    });

}); 

还有 php:

<?php
session_start();

if(isset($_POST['session_name'])){

    $session_name='airportid';
    $session_value=$_POST['---'];
    $_SESSION[$session_name]=$session_value;
}
?>

会话名称应硬编码为 airportid 。关于价值我不确定。

【问题讨论】:

  • 你喜欢什么都可以
  • 请注意,您在 ajax 请求中使用了method:"GET",而服务器似乎期待的是一个 POST 请求(来自您的 php 代码)。跨度>

标签: php jquery session


【解决方案1】:

如果必须硬编码

 session_start();
 if(isset($_POST['session_name'])){
   $_SESSION['airportid']=$_POST['session_value'];
 }

否则

$_SESSION[$_POST['session_name']]=$_POST['session_value'];

编辑,您还必须将method:"GET", 更改为method:"POST",

【讨论】:

  • session_name和session_value从何而来?
  • 从您的数据字段中,数据:{ session_name: 'airportid', session_value: $(this).val() }
猜你喜欢
  • 1970-01-01
  • 2012-05-23
  • 2021-09-10
  • 2017-09-08
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多