How to access PHP session variables from jQuery function in a .js file?
You can produce the javascript file via PHP. Nothing says a javascript file must have a .js extention. For example in your HTML:
<script src='javascript.php'></script>
Then your script file:
<?php header("Content-type: application/javascript"); ?>
$(function() {
$( "#progressbar" ).progressbar({
value: <?php echo $_SESSION['value'] ?>
});
// ... more javascript ...
If this particular method isn't an option, you could put an AJAX request in your javascript file, and have the data returned as JSON from the server side script.
Accessing a PHP session variable in jQuery file
Given a PHP file (foo.php) which contains the following:
<?php
session_start();
$_SESSION['foo'] = 'foo';
echo $_SESSION['foo'];
?>
You could use jQuery to get the value:
$.get('foo.php', function(data) {
var foo = data; // 'foo' in this case
});
I am using the $.get
AJAX shortcut method but you could also go with any of the other jQuery AJAX methods.
Get a PHP $_SESSION variable from Jquery?
You'll need to inject it, something like this:
var sessName = '<?php echo $_SESSION['name']?>';
The file containing this script must be executed by the php interpreter (i.e. a .php file)
EDIT: Conceding to Radu's point, it would be safer execute for unsanitized data:
var sessName = <?php echo json_encode($_SESSION['name']) ?>;
Access PHP SESSION value in JavaScript
I'll simply elaborate on my comments in this answer.
You say
An AJAX request to a PHP script would not be ideal — an attacker can
then edit the response to make it match with their own hashed strings.
They can edit the response, but if it's all done client side, they can still edit it.
You want to send the data hashed, then you want the client to be able to check the hash, so I'm not sure what the point in hashing would be, other than security in transport. I can't tell you what you really need, because I'm not seeing the use case here.
I do know you'll either need to go to the server for something you want to keep secret from the client. There's no security on the client side.
How to access php session in javascript file?
you can start session on your page like <?php session_start();?>
and create hidden field for session like this
<input type="hidden" name="mysession" id="mysession">
and modify javascript function some thing like this
function Result(){
var marks = 55;
document.getElementById("mysession").innerHTML= <?php echo session_id();?>;
document.getElementById("hdnmarks").innerHTML= marks;
document.getElementById('Form').submit();
}
change the Form name with your form name
Is it possible to access a SESSION variable (set in PHP) in pure JS?
It is not possible to access PHP Session Variables in .html or .js files directly. However you can print/echo PHP variables in html.
If you want these variables as JS variables that can be done but it is not a good practise to do that.
Better way of accessing PHP variables in JS is via AJAX and PHP file.
An example would be like below -
// sessionInfo.php
$sessions = array("var1" => $_SESSION['varXY'], "var2" => $_SESSION['abc']);
echo json_encode($sessions);
And in JS file -
$.ajax({url : sessionInfo.php, method: "get", dataType:"json" success: function(response){//Your session variable values here ...}});
Getting php session value in jquery mobile
Change the single quote at this line
var sessName = '<?php echo json_encode($_SESSION['schoolname']); ?>';
To double quotes
var sessName = "<?php echo json_encode($_SESSION['schoolname']); ?>";
The sessName
variable string is breaking up by using single quotes surrounding the string
and php
variable. because its stops right at ['
UPDATE:
As you say in your comments that you trying to perform PHP in .JS
files that is totally wrong, because for PHP to work you need to use .PHP
file extensions not .JS
extensions
Related Topics
PHP Create and Save a Txt File to Root Directory
Differencebetween Sessions and Cookies in PHP
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
Artisan Migrate Could Not Find Driver
How to Password Protect Folder/Page Using PHP Without a Db or Username
Scaling a Chat App - Short Polling VS. Long Polling (Ajax, PHP)
Laravel 4 - Logging SQL Queries
How to Re-Save the Entity as Another Row in Doctrine 2
Setting PHP Tmp Dir - PHP Upload Not Working
What Factors Make PHP Unicode-Incompatible
Remove Diacritics from a String
Print Newline in PHP in Single Quotes
Regular Expression to Check If the String Has Chinese Chars
Using If(Isset($_Post['Submit'])) to Not Display Echo When Script Is Open Is Not Working