0

i want to call a php function header('Location:http://xx.yy.zz/somthing') inside a javascript function,Like given below.

<?php
<script>
function mUp(obj)
 {
 //here i want call the header function
 }
</scipt>
//some code
?>

Pls help..

5
  • You can not call a specific function from PHP with javascript. You could use AJAX to fetch a page that calls the function though. Commented Sep 28, 2012 at 9:19
  • That's not how you would use the header() function. Commented Sep 28, 2012 at 9:19
  • 1
    To be honest I'd suggest back tracking a little to learn the differences between js & php before continuing coding whatever you're coding Commented Sep 28, 2012 at 9:21
  • Take a few more mintutes and see if you can find a better pattern Commented Sep 28, 2012 at 9:21
  • I want the php function because i want the session variables to be available there. Commented Sep 28, 2012 at 9:33

11 Answers 11

1

Thats not how it works.

Use:

window.location = "http://xx.yy.zz/somthing";
Sign up to request clarification or add additional context in comments.

Comments

1

Hi the following link may help you

http://forums.devarticles.com/javascript-development-22/calling-php-functions-with-javascript-3471.html

Comments

0

This is something for JavaScript, not for PHP:

<script>
function mUp(obj)
{
    document.location.href = 'http://www.example.com/';
}
</script>

Comments

0

The code similar to PHP's header() location function:

header('Location:http://xx.yy.zz/somthing');

In javascript is,

location.href = "http://xx.yy.zz/somthing";

Both does the same redirect for the users, but the former one returns a HTTP Status of 301.

Comments

0

Why don't you use directly the javascript version?

Have a look to window.location object.

Comments

0

why PHP function for redirecting ? If you want to redirect simply

document.location.href='http://xx.yy.zz/somthing';

Comments

0

There is no need to put php code. .Just try this...

<script>
function mUp(obj)
{
 window.location="http://xx.yy.zz/somthing";
}
</scipt>

Comments

0

By now you probably have noticed that If you put a php header redirect in after already sending html/any text to the client's browser, you get an error; this is because the header() function tells the server to cancel the output from the current page and redirect to another page at the server-side. so either you should find a way to execute that header function before printing any html/js to the broswer, or you could do a javascript redirect:

<script type="text/JavaScript">
   window.location.href="http://xx.yy.zz/somthing";
</script>

please comment if this isn't clear enough

Comments

0

I guess you want to do a redirect, it would be good to redirect through javascript itself , if you want to write PHP code inside, just use PHP tags in between and write the code..or echo the javascript code and write PHP code.

Comments

0

You can't execute a PHP header function inside Javascript.

Javascript runs inside the browser, While PHP runs on your server.

do something like

window.location.href = "http://xx.yy.zz/somthing";

Comments

0

Try this

header('Location:yourpage.php');

Comments

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.