Jump to content

[SOLVED] If Button is pressed


Snooble

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/36643-solved-if-button-is-pressed/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.