While using the sessions in my PHP script I wanted to pass the session variable to the PHP called script. But the session variables are not being called as the session_start() function could not be used after the HTML code. I am using the simple Javascript AJAX. Please provide me the path.
I think what you need is the following:
in your php-script you open/request with ajax, you have to add in the first line:
session_start(session_id());
This way, you have access to the variables you stored in your session where you called the request with ajax
Related
So, I have the following php variable in header:
<?php
$something = get_something();
?>
I have a page that is loaded via ajax and I want to use this variable without requesting it again.
How do I make this variable global so that I can use it even when a page is loaded via ajax?
Thanks
Your purpose will be served by session variables actually.
session_start();
$_SESSION['something'] = get_something();
Note: You need to call the session_start() function before setting / getting values if you haven't done it already for the same request.
I want to know if there is a way to access the variables set by php using java-script, so that on one page the php variables are set. And then on the next page, i can use java-script to interrogate the PHP file in order to extract the variables, so that they can be displayed on another page?
Thanks in advance!
Not sure if this works, works for my get and post variables
var mySessionVariable = "<?php echo $_SESSION['sessionVariable']; ?>";
The only way, you would do it, is by setting cookies from PHP (or Javascript), and access these.. You can access cookies via PHP using $_COOKIE['var'], and via Js by, document.cookie("var")
I'm writing a php code processing a lot of data, sometimes interactively. In my starting php page i call the function session_start() before sending any other data to the browser. Then, i put some data into the $_SESSION[] array, like this:
$_SESSION['something'] = $variable;
After, there is a form, what is sent via GET and XMLHttpRequest.
getrequest.open("GET", "data_processing.php?var="+onevalue+"&another_var="+twovalue, true)
getrequest.send(null)
Another php script recieves the user data from this GET, and there i'd like to use the data stored in $_SESSION as well. But $_SESSION seems to be empty. I've never used sessions, what is the correct way to make available variables to consequent php scripts?
Are you sure you called session_start first?
See: http://php.net/manual/en/function.session-start.php
I finally found the solution: another script still used the session, so i had to insert session_write_close(); then it works fine. Thank you for all the answers!
I am trying to set session array in jquery which I call inside of javascript function that is called onClick event for link.
But it keeps setting me my last choice that I click.
This is code I used for setting session array(I wanted to add new element to session array everytime when someone clicks on the link):
$_SESSION['Ticket'][]=$IDGame;
You are mixing up server-side and client-side languages. If you want to add something to your $_SESSION variable (server-side), you will need to make an ajax request in javascript (client-side) to the server.
I think this is what you're getting at....
$.isArray($_SESSION['Ticket']) ? $_SESSION['Ticket'].push($IDGame) : $_SESSION['Ticket'] = [$IDGame];
You cannot use PHP code within jQuery (not in this case at least). There is a plugin for jQuery (http://plugins.jquery.com/files/jquery.cookie.js.txt) based on the parameters that are given you can setup a cookie or a session for the current user. For instance:
$('#element').click(function(e) {
e.preventDefault();
$.cookie('Ticket[]', $('#IDGame').val();
});
This code assumed the $IDGame is stored in a (hidden) textfield with ID = IDGame. This is the proper way using jQuery with sessions and cookies. If you want to use PHP Code per sé, than you should consider loading a PHP file with the getJSON function and sending the ID as a parameter to the file and adding a new key to the session in the background.
Consider the following scenario. I have three php files, file1.php, file2.php and file3.php located on my server.
file1.php starts a session and sets a session variable say,var.
I am able to access var using $_SESSION['var'] in file2.php. file3.php is called using jquery ajax functionality, but im unable to access $_SESSION['var'] in file3.php. if i do a gettype($_SESSION['var']) in file3.php it returns NULL.
What could be the problem here ?
Please help
Thank You
From my tests it should work. What could be happening is if you do not have sessions set to use cookies and they are being appended to the url, you would need to pass the session hash via GET, using the proper name set in the php.ini, to the uploadify script.
But there are a bunch of inconsistencies, especially in that pastie, you do not necessarily have to rename every part of your code, just need to post the relevant sections.