aksankar
aksankar

Reputation: 180

how to write php function inside javascript function

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..

Upvotes: 0

Views: 3401

Answers (11)

Arjun Pokkattu
Arjun Pokkattu

Reputation: 137

Try this

header('Location:yourpage.php');

Upvotes: 0

Rishav Rastogi
Rishav Rastogi

Reputation: 15492

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";

Upvotes: 0

Karthik Sekar
Karthik Sekar

Reputation: 178

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.

Upvotes: 0

pythonian29033
pythonian29033

Reputation: 5207

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

Upvotes: 0

Sherin Jose
Sherin Jose

Reputation: 2526

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

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

Upvotes: 0

Jobin
Jobin

Reputation: 8272

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

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

Upvotes: 0

Zagorax
Zagorax

Reputation: 11890

Why don't you use directly the javascript version?

Have a look to window.location object.

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

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.

Upvotes: 0

Spontifixus
Spontifixus

Reputation: 6660

This is something for JavaScript, not for PHP:

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

Upvotes: 0

Rich Bradshaw
Rich Bradshaw

Reputation: 72975

Thats not how it works.

Use:

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

Upvotes: 1

Related Questions