I want to pass a value that is given in a PHP variable $_SESSION['T']
to JQuery . This variable changes its value as the "button" type inputs that I have in HTML5 are pressed . For the "Previous" input, the variable decreases (-) and of course, for the "Next" input , the variable increases (+). This is perfect.
Now what I want is that when it reaches the first record (it is achieved by clicking on the "Previous" input ) that same button is disabled and if it reaches the end (click on the "Next" input ) the same.
The problem is that I cannot achieve this from JQuery , although I can see the value from the console (for example, register 21, which is the one it starts with) when I click on the "Previous" or "Next" buttons that variable T that is in PHP (in JQuery it is named as Nro ) does not change. Therefore it seems that it is always in the same record (21), when it should change to number 20 (for "Previous" ) or 22 (for "Next" ).
This is a part of the code I have from PHP :
session_start();
if (!isset($_SESSION['T'])) {
$_SESSION['T']=21;
}else{
if(isset($_POST['Ant']) && !empty($_POST['Ant'])) {
$_SESSION['T']--;
}
if(isset($_POST['Sig']) && !empty($_POST['Sig'])) {
$_SESSION['T']++;
}
}
$T=$_SESSION['T'];
And the code I have for both buttons from JQuery is:
$('body').on('click', '#Ant, #Sig', function(e) {
e.preventDefault();
var $btn = $(e.target);
$(".Tips").hide();
$("#carga").show();
var dataString = $('#FrmBus').serialize() + '&' +
encodeURI($btn.attr('name')) + '=' + encodeURI($btn.val());
$.ajax({
url: "http://localhost:81/prueba/",type: "POST",
data:dataString,
cache:false,
success:function(data)
{
$("#carga").hide();
$(".Tips").show();
var $response=$.parseHTML(data);
$response=$('.Tips',$response).html();
$(".Tips").html($response);
$("input:button").button();
//$btn.prop('disabled', false);
var Nro = <?php echo $T?>;
if (Nro==1){
$("#Ant").prop('disabled',true);
}else if (Nro==1){
$("#Sig").prop('disabled',true);
}else{
console.log("El registro actual es el "+ Nro);
}
}});
return false;
});
Perhaps it cannot be manipulated in that way, I am talking about the line var Nro = <?php echo $T?>;
I have also tried with json_encode ()
and it does not work for me either. Is it a server side settings issue?
In the end, I solved it by adding an always inside the form in PHP
input type='hidden'
, leaving:And then the part of the call in JQuery I am left with:
Maybe it will help someone else.