I need to retrieve session variable data inside an ajax call. Here is my try.
$('#update').click(function(){
var number = $('#num').val();
var curuser = <?php echo $_SESSION['userName'];?>
if( number != '' && number.length==9 )
{
$.ajax({
data:{number:number},
success:function(data){
$('#number_add').val(number);
$('#new_data_Modal').modal('show');
}
});
}
});
Seems like var curuser = <?php echo $_SESSION['userName'];?> prevents opening my model(new_data_Modal) after button(update) click. When I remove that line it works fine(Model is opening as previous).
But alert("<?php echo $_SESSION['userName'];?>") is working and it prints the session variable as well.
Can someone show me where I made the wrong?
<?php echo $_SESSION['userName'];?>with quotes, because in your example in result you would have sth like following:var curuser = userNameValuewhich is not valid code in JS (if it would return string value). Also one suggestion (not really important) consider usinglet(mutable) andconst(immutable) insteadvar.