从一个php页面重定向到两个不同的页面

问题描述:

我有三个不同的按钮。从一个php页面重定向到两个不同的页面

第一个按钮应该重定向到Google.com,也使用Header()方法。

第二个按钮应该使用其他Header()方法重定向到stackoverfkow.com。

最后第三个按钮应该重定向到我本地机器中的页面(该文件的路径是D:\ Work Space For Practice \ Redirection \ HomePage.php)。

请任何人都可以帮助我。 在此先感谢

button.html:

<html> 
    <head> 
     <title>buttons</title> 
    </head> 
    <body> 
     <input type="button" onclick="document.location='redirector.php?q=1'" /> 
     <input type="button" onclick="document.location='redirector.php?q=2'" /> 
     <input type="button" onclick="document.location='redirector.php?q=3'" /> 
    </body> 
</html> 

redirector.php:

<?php 
if($_GET['q'] == 1) { 
    header('LOCATION: http://google.com'); 
} else if ($_GET['q'] == 2) { 
    header('LOCATION: http://*.com'); 
} else { 
    header('LOCATION: homepage.php'); 
} 
?> 
+0

非常不错的@Mohammad Ali Akbari,但它的错误提示:/Redirection/Page3.php第2行 - 未定义索引:q 注意:/Redirection/Page3.php第6行 - 未定义索引:q – Gnanendra 2011-05-10 07:04:09

+0

添加以下行: $ _GET ['q'] = isset($ _ GET ['q'])?$ _GET [ 'Q']:3; – 2011-05-10 07:36:50

+0

现在好了,代码中没有问题,而是显示了显示重定向路径的页面内容。为什么如此......? – Gnanendra 2011-05-10 07:47:53

您不能'使用标题,因为它会从您的浏览器调用您的PHP文件,而不是解释它。

您是否使用本地网络服务器?然后用Header()方法重定向到(例如)“http://localhost/HomePage.php”。

+0

我想调用里面的PHP函数,我必须调用rediredt到其他页面。 – Gnanendra 2011-05-10 06:37:30

为什么你使用header()方法呢?你可以使用javascript功能。

i.e `getRedirect(this.id)` 

并把它放在onclick事件的按钮。

,你应该检查按钮的名称或ID ..

function getRedirect(thisid){ 
if(thidid == 'first'){ 

window.location = 'www.google.com'; 

}else if(thidid == 'second'){ 

window.location = 'www.*.com'; 

}else{ 
window.location = 'localpath'; 
} 

} 

这可能是你所需要的...

感谢。

+0

Thanku @Chandresh,但我想调用一个PHP函数。在PHP函数中应该发生重定向。 – Gnanendra 2011-05-10 06:30:24

+0

好..然后fine.still不用担心..你也可以在php函数中写

+0

你能提供一个示例代码吗? B'coz我完全没有用PHP。 – Gnanendra 2011-05-10 06:56:34

你的PHP函数可能看起来像这样。

<?php 
function getredirect(btn_value){ 
    if(btn_value == 'first'){?> 
    <script>window.location = 'www.google.com';</script> 
<?php }else if(btn_value == 'second'){?> 
     <script>window.location = 'www.*.com';</script> 
<?php }else{ ?> 
     <script>window.location = 'your local path';</script> 
<?php } ?> 
} 
?> 

谢谢..

你也可以写这样的.. 你应该第一个给一个名字关闭按钮像 button.html:

<html> 
    <head> 
     <title>buttons</title> 
    </head> 
    <body> 
     <input type="button" onclick="document.location='redirector.php?q=1'" name='home' /> 
     <input type="button" onclick="document.location='redirector.php?q=2'" name='ABC' /> 
     <input type="button" onclick="document.location='redirector.php?q=3'" name='XYZ' /> 
    </body> 
</html> 

和你的PHP文件是

if(isset($_POST['Home'])) 
{?> 
    <script>window.location = 'http://google.com';</script> 
<?php 
} 
if(isset($_POST['ABC'])) 
{?> 
    <script>window.location = 'http://*.com';</script> 
<?php 
}