0

How do I open a php page from javascript function ?

  1. window.location = "http://www.google.com";
  2. window.location.href = "http://www.google.com";
  3. location.href = "http://www.google.com";

None of them is working.
Even put in php.ini

allow_url_fopen = on

What is wrong at my approach?

6
  • 4
    can you please paste the full code ? Commented Sep 27, 2012 at 9:21
  • alert something and make sure code is in flow. Commented Sep 27, 2012 at 9:22
  • 1
    window.location is right approach. If you are trying it in iframe or FB app then try window.top.location Commented Sep 27, 2012 at 9:23
  • Are they within the <script>-tags? Commented Sep 27, 2012 at 9:25
  • Are you using frames? With frames you end up with a cross domain request which is blocked by some sites (like google). Commented Sep 27, 2012 at 9:28

4 Answers 4

1

If you have this javascript embedded within <script> tags it should work:

<script type="text/javascript">
  window.location = "http://www.google.com";
</script>

However, you cannot cause a PHP script to be included within a page you are writing with thsi javascript function: It will only redirect the browser to google or whatever other page you have set in window.location. You can include a PHP script from within a PHP page using the PHP include() or require() functions:

<?php
  include('file.php');
?>
Sign up to request clarification or add additional context in comments.

5 Comments

ist a function in the header ... tags are there for all functions
window.location = "../../member/en/member.php";
As long as the page is accessible normally (for example just writing the address into the address bar and it is working there), then your window.location = "../../member/en/member.php"; should work. I would check to make sure that the location is accessible normally.
full code :function callmemberpage() { alert("callmemberpage was called"); window.location = '../../member/en/member1.html'; }
Ignore what I said about headers. That is for the PHP header() function. :P Is it still not working?
1
window.location = "http://www.google.com";

Should work..

I think your javascript code contain some error so it is not working.

Comments

0

So php.ini's line allow_url_fopen has nothing to do with this. It is to allow or not a url to be given to the fopen function of PHP.

Here what you want to do is to redirect a user to another page using window.location = '/page.php'.

Unless by open you meant getting loading inside the actual page, in this case have a look at the mdn documentation about XHR. And you should be using jQuery's load function.

Comments

-1
window.location.href = "http://www.google.com";

That will only redirect the user to Google.

But you can't load a page in a JS variable, every browser have a "cross-domain" restriction (unless the page is on the same domain as you JS file & your visitor).

3 Comments

CORS issue only occur with XHR.
after the users login and his status is known i want to foreward the user to different new pages .... used google to avoid that my site is not working or not found
window.location = "../../member/en/member.php"; that is what i need or window.location = "../../admin/en/admin.php";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.