dagogodboss Posted July 7, 2014 Share Posted July 7, 2014 OKKK! good day please i have little problem that is now a mighty one before me. I added a login form to a page using Iframe modal class i got from tinybox2. I have been able to login the user and in use the HEADER function to redirect the user to their user_page but the direction is done in the I-frame and not the parent window. Now my problem is how do i close the iframe and still redirect the user to the next page. I tried some javascript but all to no avail . THE LOGIN AND I-FRAME PAGE SCRIPT <?php if(empty($errors)=== true){ $login=login($username, $password); if($login === false){ $errors[]='INVALID LOGIN DETAILS.... PLEASE CHECK YOUR DETAILS AND TRY AGAIN.'; } else{ $_SESSION["user_id"]=$login; ?> <script> parent.window.close();</script> <?php header("location:user_page.php"); } } if(empty($errors)=== false){ $_SESSION["errors"]=$errors[0]; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Login</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <link href="css/signin.css" rel="stylesheet" type="text/css"> </head> <body> <div class="account-container"> <div class="content clearfix"> <form action="#" method="post"> <h1>Member Login</h1> <div class="login-fields"> <p>Please provide your details</p> <div class="field"> <label for="username">Username</label> <input type="text" id="username" name="username" value="" placeholder="Username" class="login username-field" /> </div> <!-- /field --> <div class="field"> <label for="password">Password:</label> <input type="password" id="password" name="password" value="" placeholder="Password" class="login password-field"/> </div> <!-- /password --> </div> <!-- /login-fields --> <input class="login-fields_submit" type="submit" name="login" value="Log In"> </form> <?php if(isset($_SESSION["errors"])){?> <h1 style="background:darkred;margin-bottom:-27px;font-size:9px !important;"> <?php echo $_SESSION["errors"]; unset($_SESSION["errors"]);?></h1><?php }?> </div> <!-- /content --> </div> <!-- /account-container --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/289530-close-iframe-and-redirect-user-to-user_page/ Share on other sites More sharing options...
Ch0cu3r Posted July 7, 2014 Share Posted July 7, 2014 PHP cant target elements in browser so using header will never work. Your only hope is to use javascript to target the parent window and change its location, something like <script> parent.window.location = 'target-page.php' </script> But using iframes is so old school. The more modern method is to use ajax to accomplish this. Whereby when the user clicks the login/submit button you'd submit the forms contents via ajax. Your PHP script will only serve a success response if the users credentials where valid. In your JavaScript you'd then listen out for this response and decide what to do when the user logins successfully, such as redirect the page using javascript or dynamically load in the restricted elements into the page without having the page reload (like when you click the edit button on your post). There are javascript frameworks out there which helps to aid in this process. JQuery is more common (there are others) or an alternative would be something AngularJS for creating single page applications. Quote Link to comment https://forums.phpfreaks.com/topic/289530-close-iframe-and-redirect-user-to-user_page/#findComment-1484151 Share on other sites More sharing options...
dagogodboss Posted July 7, 2014 Author Share Posted July 7, 2014 Ch0cu3r you are truely the boss. Thank you so very much it works perfectly for what i want. But is the closing not working. Quote Link to comment https://forums.phpfreaks.com/topic/289530-close-iframe-and-redirect-user-to-user_page/#findComment-1484186 Share on other sites More sharing options...
dagogodboss Posted July 7, 2014 Author Share Posted July 7, 2014 Actually Boss i don't really like framework. Could you please point me to a good tutorial for novice in ajax. Google is longer specific to many junk programing. May be a book or video tutorials on the Ajax concept. Quote Link to comment https://forums.phpfreaks.com/topic/289530-close-iframe-and-redirect-user-to-user_page/#findComment-1484189 Share on other sites More sharing options...
QuickOldCar Posted July 8, 2014 Share Posted July 8, 2014 https://developer.mozilla.org/en-US/docs/AJAX Quote Link to comment https://forums.phpfreaks.com/topic/289530-close-iframe-and-redirect-user-to-user_page/#findComment-1484249 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.