Majk Johnoson
Majk Johnoson

Reputation: 39

How to save the value of the selected item of a dropdown list in a session

I want to save the selected item of a dropdown-list in a session. My call for the session looks like:

$('#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");
    });

}); 

And php :

<?php
session_start();

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

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

The name of the session should be hardcoded as airportid . About the value im not sure.

Upvotes: 0

Views: 74

Answers (1)

keja
keja

Reputation: 1363

if it has to be hardcoded

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

or else

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

edit, you have to also change method:"GET", to method:"POST",

Upvotes: 2

Related Questions