Welcome to WebmasterWorld Guest from 23.22.182.29

Forum Moderators: open

Message Too Old, No Replies

how to pass php variable to ajax?

     
4:52 pm on Jan 30, 2013 (gmt 0)

New User

joined:Jan 30, 2013
posts: 33
votes: 0


Please, explain or give link how to pass php variable to ajax?

php $ifloggedin = $_SESSION['loggedin'];

in ajax
if $ifloggedin equals to 1, then redirect
like here
if($ifloggedin == 1) {
window.location = "login-success.php";
}

but it does not work... i do not understand
12:16 am on Jan 31, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


The value from $_SESSION['loggedin'] needs to be transferred from the server PHP to the client by including it somewhere in the PHP generated HTML or PHP generated Javascript with
<?php echo $_SESSION['loggedin']; ?>

Within Javascript maybe

var ifloggedin = <?php echo $_SESSION['loggedin']; ?>;

OR

if(<?php echo $_SESSION['loggedin']; ?> == 1) {
window.location = "login-success.php";
}


Within HTML maybe

<script>
var ifloggedin = <?php echo $_SESSION['loggedin']; ?>;
</script>
9:37 am on Jan 31, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


I'd make sure to properly escape any user generated input before outputting it in a script context. XSS is really close by if you don't.

See rule 3: [owasp.org...]
9:43 am on Jan 31, 2013 (gmt 0)

New User

joined:Jan 30, 2013
posts: 33
votes: 0


Thanks for information.
Then seems for login form better not use ajax...
Better use only php for processing.