bonaca bonaca - 3 months ago 19
Ajax Question

change php session variable using ajax

$('.folder').on('contextmenu', function(ev){
ev.preventDefault();
var a = $(this).attr('data-id');
var b = $(this).text();
$('.marked').removeClass('marked');
var c = $('#path').html();
var d = '<span class="spant" data-id=' + a + '>' + b + '</span>';
var e = c + d;
$.ajax({
url: 'params.php',
type: 'post',
data: {
'path': e,
'par': a
},
success: function() {
location.reload();
}
});

});


params.php

session_start();
if(isset($_POST['path'])) {
$_SESSION['path'] = $_POST['path'];
}
else{
$_SESSION['path'] = '<span class="spant" data-id=0>HOME</span>';
}

if(isset($_POST['par'])) {
$_SESSION['par'] = $_POST['par'];
}
else{
$_SESSION['par'] = 0;
}


admin.php

<?php
include ("params.php");
echo $_SESSION['path'];
echo $_SESSION['par'];
?>


Result of echoing is old values, as
$_POST
variables are not set.

How can I get new values, set in javascript code ?

Answer

You made a mistake in the request, use: method: "post" instead of type: "post", so no wonder the post var's aren't set :)

$('.folder').on('contextmenu', function(ev){
    ev.preventDefault();
    var a = $(this).attr('data-id');
    var b = $(this).text();
    $('.marked').removeClass('marked');
    var c = $('#path').html();
    var d = '<span class="spant" data-id=' + a + '>' + b + '</span>';
    var e = c + d;
    $.ajax({
        url: 'params.php',
        method: 'POST',
        data: {
            'path': e,
            'par': a
            },
        success: function() {
            location.reload();
        }
    });

});

Hope this will fix it ;)

Cheers!

Comments