简体   繁体   English

如何在javascript函数中编写php函数

[英]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. 我想在javascript函数中调用一个php函数头('Location:http://xx.yy.zz/somthing') ,如下所示。

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

Pls help.. 请帮忙..

Thats not how it works. 那不是它的工作原理。

Use: 采用:

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

You can't execute a PHP header function inside Javascript. 您无法在Javascript中执行PHP标头功能。

Javascript runs inside the browser, While PHP runs on your server. Javascript在浏览器中运行,而PHP则在您的服务器上运行。

do something like 做点什么

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

This is something for JavaScript, not for PHP: 这适用于JavaScript,而不适用于PHP:

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

我想你想做一个重定向,通过javascript本身重定向会很好,如果你想在里面编写PHP代码,只需在中间使用PHP标签并编写代码。或者回显javascript代码并编写PHP代码。

The code similar to PHP's header() location function: 该代码类似于PHP的header() location函数:

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

In javascript is, 在javascript中,

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. 两者都为用户执行相同的重定向,但前者返回HTTP状态301。

Why don't you use directly the javascript version? 为什么不直接使用javascript版本?

Have a look to window.location object. 看看window.location对象。

why PHP function for redirecting ? 为什么PHP函数重定向? If you want to redirect simply 如果你想简单地重定向

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

There is no need to put php code. 没有必要放PHP代码。 .Just try this... 试试这个......

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

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; 到目前为止,您可能已经注意到,如果您在已经将html /任何文本发送到客户端的浏览器后将php头重定向,则会出现错误; 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. 这是因为header()函数告诉服务器取消当前页面的输出并重定向到服务器端的另一个页面。 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: 所以要么你应该找到一种方法来执行该头功能之前打印任何html / js到broswer,或者你可以做一个javascript重定向:

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

please comment if this isn't clear enough 如果不够清楚,请评论

尝试这个

header('Location:yourpage.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM