Solved

re-load page

Posted on 2013-11-27
7
359 Views
Last Modified: 2013-11-27
I am using jquery to send data to a php file using get.

                                <script>
                                function login(myform) {
                                        $.get('login.php',
                                        $(myform).serialize(),
                                        function(php_msg) {
                                                $("#login-scr").html(php_msg);
                                        });
                                }
                                </script>

Open in new window


The code passed the data just fine. On my login.php file after I validate there login I do 2 things.

1. If the login is incorrect I display the login page again with a error message. This works with no problems.

2. Or if login is success I set sessions then use a meta refresh tag to re-direct the visitor to the URL they were before login. The meta refresh is not working.

print "<meta http-equiv=\"refresh\" content=\"2;url=$_SESSION[uri]\"> \n";

Open in new window


Error reported from FireBug:


SyntaxError: syntax error
      

<meta http-equiv="refresh" content="2;url=http://www.liveabo

jquery-1.9.1.js (line 611)
0
Comment
Question by:Robert Saylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 39681091
I don't think you can use a meta-refresh by ajax. The meta tag is only read on the initial page load. I think you should probably do the page reload entirely with javascript. For example:

function login(myform) {
                                        $.get('login.php',
                                        $(myform).serialize(),
                                        function(php_msg) {
                                                if (valid_login)
                                                     window.location.replace(url);
                                                else
                                                     $("#login-scr").html(php_msg);
                                        });
                                }
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 39681099
I was kinda thinking meta refresh was not supported.

On my php script if the login is valid what would I call back to the parent so it knows it's valid to perform the refresh?
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 39681158
there are a couple of ways to do that. The easiest way is for your php script to return a url if the login is successful and a web page if it is not.

so in the above, you could have something like this:

                                                if (php_msg.substring(0,4) == "http")
                                                     window.location.replace(php_msg);
                                                else
                                                     $("#login-scr").html(php_msg);
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 7

Author Comment

by:Robert Saylor
ID: 39681733
PERFECT!!!!! Now all I need to do is show a quick success message then re-direct. I should be able to figure that one out. This one is going into my tool belt as I experienced this on another project as well.
0
 
LVL 7

Author Closing Comment

by:Robert Saylor
ID: 39681738
THANK YOU and have a happy holiday!
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 39681781
Here is the updated JQ with the delay: Works perfectly!

                                <script>
                                function login(myform) {
                                        $.get('login.php',
                                        $(myform).serialize(),
                                        function(php_msg) {
                                          if (php_msg.substring(0,4) == "http") {
                                             $("#login-scr").html('<span class="details-description"><br><font color=green>Login successfull. Loading please wait...</font><br></span>');
                                             $("#login-scr").delay(2000);
                                             window.location.replace(php_msg);
                                          } else {
                                             $("#login-scr").html(php_msg);
                                          }
                                        });
                                }
                                </script>

Open in new window

0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 39681806
Sorry, the delay code did not work but the setTimeout did. Tested in firefox and Safari. On a mac here but IE "should" work the same.

                                <script>
                                function login(myform) {
                                        $.get('login.php',
                                        $(myform).serialize(),
                                        function(php_msg) {
                                          if (php_msg.substring(0,4) == "http") {
                                             $("#login-scr").html('<span class="details-description"><br><font color=green>Login successfull. Loading please wait...</font><br></span>');
                                             //$("#login-scr").delay(4000);
                                             setTimeout(function()
                                                {
                                                window.location.replace(php_msg)
                                                }
                                             ,2000);
                                          } else {
                                             $("#login-scr").html(php_msg);
                                          }
                                        });
                                }
                                </script>

Open in new window

0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question