Solved

re-load page

Posted on 2013-11-27
7
354 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 6

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

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 6

Author Closing Comment

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

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 6

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now