Question:
I have a problem related to session variables. I need it to take the value of the link that is clicked:
<!-- Submenu Paciente -->
<ul class="nav nav-tabs" role="tablist">
<li role="MenuPatient" id="ginecologiaMenu" class="active">
<a href="#ginecologia" aria-controls="ginecologia" role="tab" data-toggle="tab" onclick="<?php $_SESSION['ListMenu'] = 'ginecologia'; ?>">Ginecología</a>
</li>
<li role="MenuPatient" id="obstetriciaMenu">
<a href="#obstetricia" aria-controls="obstetricia" role="tab" data-toggle="tab" onclick="<?php $_SESSION['ListMenu'] = 'obstetricia'; ?>">Obstetricia</a>
</li>
<li role="MenuPatient" id="antecedentesMenu">
<a href="#antecedentes" aria-controls="antecedentes" role="tab" data-toggle="tab" onclick="funcionAntecedentes() <?php $_SESSION['ListMenu'] = 'antecedentes'; ?>">Antecedentes</a>
</li>
<li role="MenuPatient" id="documentosMenu">
<a href="#documentos" aria-controls="documentos" role="tab" data-toggle="tab" onclick="<?php $_SESSION['ListMenu'] = 'documentos';?>">Documentos</a>
</li>
</ul>
So that later you can use that variable so that when you return to the page you return to the previous tab depending on the value of the variable.
The problem is that it always takes the value of the last link: Documents.
Answer:
Well this could be a possible solution, change your session variable in the backend part, so your html code would be more or less like this:
<!-- Submenu Paciente -->
<ul class="nav nav-tabs" role="tablist">
<li role="MenuPatient" id="ginecologiaMenu" class="active">
<a href="#ginecologia" aria-controls="ginecologia" role="tab" data-toggle="tab" onclick="modificarValor(1)">Ginecología</a>
</li>
<li role="MenuPatient" id="obstetriciaMenu">
<a href="#obstetricia" aria-controls="obstetricia" role="tab" data-toggle="tab" onclick="modificarValor(2)">Obstetricia</a>
</li>
<li role="MenuPatient" id="antecedentesMenu">
<a href="#antecedentes" aria-controls="antecedentes" role="tab" data-toggle="tab" onclick="modificarValor(3)">Antecedentes</a>
</li>
<li role="MenuPatient" id="documentosMenu">
<a href="#documentos" aria-controls="documentos" role="tab" data-toggle="tab" onclick="modificarValor(4)">Documentos</a>
</li>
</ul>
At the top or in an external js
file place the following function:
function modificarValor(valor){
var url = 'server.php';
$.ajax({
type: 'POST',
url: url,
data: {valor},
success:function(data){
console.log("tu varible de sesion tiene el valor de "+ data);
}
});
}
And in your server.php
file you place the following, where it will only take care of modifying your session variable depending on the value you pass to it.
$valor = $_POST['valor'];
session_start();
switch ($valor) {
case 1:
$_SESSION['ListMenu'] = "ginecologia";
echo 'ginecologia';
break;
case 2:
$_SESSION['ListMenu'] = "obstetricia";
echo 'obstetricia';
break;
case 3:
$_SESSION['ListMenu'] = "antecedentes";
echo 'antecedentes';
break;
case 4:
$_SESSION['ListMenu'] = "documentos";
echo 'documentos';
break;
}
You can improve the code much more, this is just to give you an idea of how you can do it. Greetings.