qadenza
qadenza

Reputation: 9293

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 ?

Upvotes: 0

Views: 2023

Answers (2)

Evochrome
Evochrome

Reputation: 1215

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!

Upvotes: 1

Colin
Colin

Reputation: 361

When you include params.php it's resetting the values back to the else conditions in your two if blocks.

Upvotes: 0

Related Questions