Snooble Posted February 1, 2007 Share Posted February 1, 2007 Hello people, I have a button that when pressed i want to check if something is true. For example: <?php if(isset($_SESSION['myemail'])){ header(Location:purchase.php); } else{ header(Location:login.php); } ?> But i need that on button click. How can i do that? So, if the user is logged in take them to the purchase page, if the user isn't take them to the login page. Thanks in advance Snooble Quote Link to comment https://forums.phpfreaks.com/topic/36643-solved-if-button-is-pressed/ Share on other sites More sharing options...
obsidian Posted February 1, 2007 Share Posted February 1, 2007 IMHO, the best way to do that would be to check in the form handler whether or not the session variables are set and redirect appropriately. However, if you are wanting to do this via javascript, you'll have to allow the PHP to assign the value to a javascript variable. Something like this should do the trick: <script type="text/javascript"> function checkMySession() { var sessIsSet = <?php echo isset($_SESSION['myemail']) ? "true" : "false"; ?>; if (sessIsSet) { /* Your session variable is set, return true or redirect as needed */ } else { /* Your session variable is not set, return false or redirect as needed */ } } </script> Then, simply apply that function to an onclick event handler. Quote Link to comment https://forums.phpfreaks.com/topic/36643-solved-if-button-is-pressed/#findComment-174646 Share on other sites More sharing options...
Snooble Posted February 1, 2007 Author Share Posted February 1, 2007 i swear this can be done in php alone? am i wrong, do i have to use javascript? Thanks Snooble Quote Link to comment https://forums.phpfreaks.com/topic/36643-solved-if-button-is-pressed/#findComment-174648 Share on other sites More sharing options...
obsidian Posted February 1, 2007 Share Posted February 1, 2007 i swear this can be done in php alone? am i wrong, do i have to use javascript? Thanks Snooble If you're wanting to do it with an onclick handler, you must use javascript. If you're wanting to use PHP alone, you need to use my suggestion of letting your form handler (whatever page the action of your form is set to) check the session variables and redirect accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/36643-solved-if-button-is-pressed/#findComment-174650 Share on other sites More sharing options...
Snooble Posted February 1, 2007 Author Share Posted February 1, 2007 oh yeah. I'm such a dickhead sometimes. So link to "checkcheckcheck.php" or whatever. And on that page have my code. Easy enough. lol Snooble (Thanks, takes 2 pairs of eyes sometimes) Quote Link to comment https://forums.phpfreaks.com/topic/36643-solved-if-button-is-pressed/#findComment-174653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.