I have a question regarding passing data to a php session when an the element is clicked. the data that needs to be passed is looped in a foreach loop which is what i am having difficulty with.
I want the page to refresh in order to change the language on the entire page. I have ajax examples below which arent the logical solution to my problem(as pointed out by a few users)
<?php foreach($languages as $single_language){?>
<li>
<a class="d-flex align-items-center <?php if($_SESSION['languages_id'] == $single_language["id"]){ echo "disabled"; }?>" data-id="<?php echo $single_language["id"]?>" data-direct="<?php echo $single_language["directory"]?>" id="language_selection_<?php echo $single_language["id"]?>" href="#">
<img src="<?php echo $single_language['short_name']. '.png' ?>" />
<span class="ps-2"><?php echo $single_language["name"]; ?></span>
</a>
</li>
<?php } ?>
Data stored in languages that i need to pass is id and directory. E.g. $single_language["id"] = 4
and $single_language["directory"] = dutch
.
There are 3 'languages' that are selectable and when selected should display the according flag.
Ive tried using ajax:
$('[id^=language_selection_]').click(function(){
var Lid = $(this).data('id');
var direct = $(this).data('direct');
$.ajax({
type: 'post',
success: function(response){
<?php
$_SESSION['languages_id'] = $_POST['Lid'] ;
$_SESSION['language'] = $_POST['direct'];
?>
}
});
which did not store any data into my session. the second option of ajax i used was:
$('[id^=language_selection_]').click(function(){
var Lid = $(this).data('id');
var direct = $(this).data('direct');
$.ajax({url: "ajax_sidebar.php?Lid="+Lid+"&direct="+direct, success: function(result){
alert('test');
}
});
and ajax_sidebar.php has
<?php
session_start();
$_SESSION['languages_id'] = $_GET['Lid'];
$_SESSION['language'] = $_GET['direct'];
?>
which also did not seem to work, it did not change the session at all and never reached the alert.
when i put this within the foreach the code does work and the last value out of the 3 gets saved, not the one that gets clicked:
$_SESSION['languages_id'] = $single_language["id"] ; $_SESSION['language'] = $single_language["directory"];
Ive seen a few other options online which all seemed to give me an error one way or another so im hoping that someone can help me out.