Jquery ajax set some session variables then trigger click - php

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

Related

How to display a complete string that is passed to another ajax page

I am new to ajax, I dont know how i can check the values that are passed to another page in ajax, Actually i want to confirm the values passed to another page using alert.
here is the variable which i want to check with alert
data: 'user='+user_id+'&project='+project+'&date from='+date_from+'&date to='+date_to+'&stat='+status+'&leave_type='+leave_type,
If I am correct, if you wanna verify the data that has been posted, you can use var_dump() function.
<?php
var_dump($_REQUEST);
?>
Ajax is a call to the server and a response from the server happening without a page reload. There is no second page that your ajax request is making a call too. If you want to confirm the variables passed to the server side, you can check the network tab in the developer console of your browser.
https://developer.chrome.com/devtools/index
https://developer.mozilla.org/en-US/docs/Tools

ajax : session need page reload to get the latest value

Hello am implementing a jquery site and i dont want the user to wait for page reloading after every click so a use #p2 to navigate between pages. I use ajax to prevent page reloading and i get my parameter from a response.php page with $_GET method...after this i store the $_GET['value'] in a session in order to use it to the page2...but this work only if i refresh the page. Otherwise the session variable has the previous value before page reloading.
My question is simple...how to get the session latest value without page reloading...?
Or is there any way to pass parameters from page 1 to page 2 without reloading?
Thanks in advance
The code is shown below:
function send_an_article_id_to_php(an_article_id)
{alert(an_article_id);
$(document).on
(
"click", "#" + an_article_id ,function()
{
$.ajax(
{
type: "GET",
url: "../get_an_article_id.php",
data: { cmd2 : $(this).attr("id") },
success: function(response)
{
$("#response3").html(response);
}
} /*end of ajax }*/
);/*end of ajax );*/
}/*end of click event*/
);/*end of document*/
}/*end of function katigoria*/
Sessions have server side processing. You can simply sent request to server using ajax and modify or change session info there. After your desired result you can get print these values and send them back to client side.
You can print value in form of json and make html from these values on client side or simply you can make HTML in you ajax file, sent it to your client and place in your desired location using client side scripting.

jquery.post doesn't create a session

I hava a strange problem. I'm using jquery $.post() to send/recieve vars from a PHP script.
JavaScript:
$("#r_submit").click(function()
{
$.post("http://" + server + "/msws/",
{
action: "register",
sub_action: "register_validate"
},
function(json)
{
json = $.parseJSON(json);
alert(json.cell_is_good);
});
});
PHP:
http://textuploader.com/?p=6&id=S7zDD
Problem:
If I run the code on my pc (WAMP) it works fine,
but if I upload it to my server (justhost) then it dosn't keep the session
if I alert the session when I create it, it is there, but when I try to get the session
later, it's gone,
I think it has to do with the fact that the server thinks that the browser was closed, so it destroys the session?
Thank you :)
You are starting your session without a session id. For this to work your PHP has to be configured to use transparent session id, which is deactivated by default. So you will have to start the session in the script that has the AJAX-call, and submit the session_id() in the POST request, and start the session in your PHP something like this:
if (!isset($_POST['SID'])) {
die('{}');
}
session_start($_POST['SID']);
Also I would leave out the host-part of the url you are calling in your $.post to avoid cross-domain ajax call problems (also this most likely will not fix your problem). It will work only on the same server, so you can write the ajax call like this:
$.post("/msws/", //...
check if you have multiple session cookies.
i've the same situation with codeigniter and i found that every new page creates its own session

setting array session value inside jquery

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.

ajax request/php session vars

I'm fairly new to javascript/ajax so bear with me. I'm attempting to send a form field value as a POST variable to a php script, and have that php script set a session variable. I don't need to update any content on the page that I'm calling the javascript script from. So here is what I'm doing...
Here is my javascript stuff:
xmlhttp.open("POST","ajax/create_employee_session_handler.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("blah=1");
window.location="newpage.php";
And here is my php code (simplified):
session_start();
if($_POST)
{
$_SESSION['blah'] = $_POST['blah'];
}
And for some reason, when I redirect to the next page, I get no $_SESSION['blah']. Any suggestions on this would be appreciated.
You are starting the AJAX request, but you are not giving it a chance to finish (The "A" in Ajax stands for "Asynchronous" so when you do the location() the request is still running.) Chances are the Ajax script never gets called.
Put the part switching the page's location into the Ajax request's success callback to make sure it works out before redirecting.

Categories