Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.