Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

re-load page

Posted on 2013-11-27
7
Medium Priority
?
361 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
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

972 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