ajax request/php session vars - php

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.

Related

PHP Header Redirect Does not Work Within The Ajax Request

After user's filled the input fields and has submitted I use an ajax request to complete the sign up process.In this register.php I'd like to redirect the user to another page after I inserted their info to the database. Everything is fine and nothing is outputted until header() function. The code below does not redirect and as if nothing happened the callback function of ajax request is processed. I am really stuck here, I don't know what to do.I can try javascript solutions but I'd prefer php solution over that one.
Code:
header("Location: http://localhost/profil.php", true, 307);
exit();
You can use JavaScript to redirect in the ajax success callback function:
success: function(data) {
window.location.href = 'http://localhost/profil.php';
}
It can't be done with a PHP header redirect, because that will only redirect the ajax call, not the original browser window.

Jquery ajax set some session variables then trigger click

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

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

php redirection headers not sent through ajax call

i am trying to send header from a php file that is called with ajax. problem is, when i validate all information with php, i want to redirect to another page but headers dont work! any ideas?
what the code does:
//------------- index.php
on click of input, function passes <input> values through $_POST to login.php
if($_SESSION[superman]==true){redirects to index_main.php}
//------------- login.php
all it does is validate forms and if login and password are correct, it sets $_SESSION [superman]=true
The redirect will only redirect the Ajax call! If you want to change the displayed page in the browser, you'll need to redirect using Javascript once the Ajax call completes.
Make your Ajax call return a value indicating successful login (for example the string true or maybe the URL to the page you want to load)
Check the return value of the Ajax call. If success, change location.href using Javascript.

access post vars JS or PHP?

I am using jquery to post vars to a php script.
Is it possible to access these vars once the script has posted? If I post the data to the php script, is the only way to access it in the html/js that i posted it from, to send it back from the php script as json?
I cant seem to do it in JS, but even php will not work?, Sorry correction i can access the post vars in the php page, but not in the html/js page i posted from
Any ideas how to access posted vars from the page thats doing the posting?
update: yep to be a bit clearer, i am using a html page with javascript to post to a php page, i would like to access the posted vars in the html javascript page. I tried outputting $.post and $.ajax and these just show a long function.
Cheers
Ke
How are you submitting your elements to php page? If you are doing everything fine and using ajax (see jquery post method) for example, you can access the php variables normally with $_POST['var_name'].
Make sure that:
Your form method type is set to POST
Your ajax request goes successful
You have specified the correct path to your server side script
In PHP, the data should be accessible through the $_POST array, just like if you posted to the script using a form (whether you make an AJAX request or a normal request through your browser, the server behaves the same). If they're not there, perhaps you actually sent your data by GET instead (you could check $_REQUEST, but it's better, and more secure, to know what method your data will be coming in), or else your AJAX request failed.
I don't recommend using $_REQUEST to post something back to your site. If someone changes their $_REQUEST vars on you, then you have an opening for cross site scripting.
Push all your vars to $_SESSION and post them as you see fit but only after they have been purified. That way even if you make some modifications to them after the fact you can rely on the source, which is in the $_SESSION. However if you are trying to perform actions after a page has executed you are straying outside the boundaries of PHP's limitations. People do it all the time with things like Jquery but it doesn't make it right.
Warning: if you allow accessing and process of vars after PHP has finished printing the page, then you run the risk of enabling attacks on your code.
I assume that you are using $.ajax or $.post to do this.
Just keep your data in local variables. There i sno reason why you should lose the posted data, or your php not being able to access it.
If you post code, maybe someone can help better.
In your php function, you can use this:
function foo() {
//do something
echo json_encode($var);
}
I use .ajax, use dataType: "json". The success attribute would be:
$.ajax(
{
url: 'ajaxfile.php',
type: "POST",
data: ($("#myForm").serialize()),
dataType: "json",
success: function(data)
{
//Insert your logic to handle the vars from php
}
});

Categories