我在php中使用header
函数来重定向到表单提交的另一个页面,以避免将数据重新提交给mysql。它工作得很好,直到我用javascript创建了一个导航菜单,这将一些代码放入页面的标题部分。之后,当我提交表单数据时,页面没有重定向。有办法解决这个问题吗?
代码如下::
<script type="text/javascript">
<!--
(function(){
var menu1=new Scl.Menu(130);
menu1.addItem('blabla','sort.php');
menu1.addItem('hahaha','view.php');
menu1.addItem('uhuhuh','list.php');
menu1.dockTo('menu1');
-->
</script>
</head>
:
header("Refresh: 0; url=http://localhost/site/main.php");
您的javascript代码似乎缺少一些右括号和圆括号:
<script type="text/javascript">
<!--
(function(){
var menu1=new Scl.Menu(130);
menu1.addItem('blabla','sort.php');
menu1.addItem('hahaha','view.php');
menu1.addItem('uhuhuh','list.php');
menu1.dockTo('menu1');
})(); // added
-->
</script>
为什么不用Location
代替url
和Refresh
呢?
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>