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.
Related
I have a page that has tabs on it. each of the tabs have a few forms on it. On the form, a user can set a filter. for example "Show me (10,25,100) result"
The when user enters a value, i fire off an ajax call to a php script that sets session variables
$_SESSION['filter'] = $_POST['filter'];
The success of the ajax call triggers the tab click to get to the form the user is on:
$.ajax({
type:"POST",
data:"filter="+filter,
url:"actions/Tickets/filters.php",
success:function(result){
$('#someTab').trigger('click');
}
});
The problem I am running into is if I
print_r($_SESSION);
on the page that the form is on, i do not see any changes to the $_SESSION['filter'] value.
What i think is happening is the ajax isnt waiting for the script to finish, so the SESSION var never gets set. asynch isnt an option. how can i acheive this?
How can i use an ajax script to call a php file to set SESSION variables then trigger a click event on nav tabs.
As you have stated in the comment that you set the session using:
$_SESSION['filter'] = $_POST['filter'];
Than, I think the issue is, when you set the session in php uisng an ajax call, than you can only get the updated session by using an another ajax call or after page refresh. So make an another ajax call and see what happens.
You cann't assign session in javascript because javascript occurs on client side and session stored at server side.
So , It's not possible you can use php variables in js like.
<?php
$_SESSION['filter'] = "foo";
?>
Then you can use this session variable in js. But you can't do vice versa. You can't assign session from js to php.
You can set value from js to html using jquery but this scenario isn't possible.
Edit :-
So, set session variable in filter.php.
$_SESSION['filter'] = "foo";
After then when result is retured reload the page. Because Page must be reload for session set. I'm not sure for it but may be it works.
$.ajax({
type:"POST",
data:"filter="+filter,
url:"actions/Tickets/filters.php",
success:function(result){
$('#someTab').trigger('click');
location.reload(); // reload the page
}
});
Ofcourse you cant assign like that because js is client side script & php is server side, you can assign php variable to js variable but not vice versa
You need to use this:
$_SESSION['filter'] = $_POST["filter"];
This post may help you:
Set php session via ajax
My code,
Using below code ineed to store catlog_id in a session but it is not working
$(document).ready(function() {
$("#catlog").click(function(event) {
var catlog ="<?php $this->session->set_userdata('catlog_id',"+$('.catlog_id').val()+")?>";
});
});
Your JavaScript is executed on the client machine and there is no PHP interpreter there. Any code that you create on the fly through JS must be HTML, CSS, JS. Setting cookies can be done through JS, look for a cookie plugins for jQuery.
A good way to access client data is to use cookies.
You can store with javascript/jquery data in a cookie and then you can it later access with php $_COOKIE
You can use the firefox plugin firebug to track the cookie changes.
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")
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
At the click of a button an ajax POST is made to my php script which has the following code
<?php
$number = $_POST["id"];
$myarray[$number] = $_POST["marker"];
?>
The two POST entries are id and marker. I was hoping that each click of the button by the end user would construct an increasingly larger array called $myarray, because $number usually changes.
Instead what happens is each click of the button destroys the original $myarray and creates a new $myarray with only one data pair (the newly sent $number and $_POST["marker"]).
How can I code it so the array is built up with each click of the button?
HTTP is a stateless protocol, so there is no way for the server to know about $my_array after each request. PHP just generates some HTML and the server serves that generated HTML.
You could store the info on the client side in javascript, though.
As for your comment-question:
No, it wouldn't. The solution really depends on your use case, if you need information to be available for later use, you'd have to store it in a database or file. If only for the current browsing page, store it in the user's browser with javascript. Storing it as session variable is another option.
You need to store it in a session. Documentation.
Basically,
session_start();
$myarray = $_SESSION['myarray'];
//work with $myarray here
//store it back in the session
$_SESSION['myarray'] = $myarray;
push the data onclick using javascript. Then try to pass the whole queue on each click