2014-09-30 89 views
-1

我有關於在PHP
重定向的問題,我有2頁
- 第一頁的索引
- 兩頁的過程編輯或edit.php
如何在條件重定向用PHP

我的問題是..
如果我點擊編輯,他肯定在edit.php和如果我點擊保存edit.php
如果我對重定向
header('location:index.php');

012碼

如何找回或重定向到索引,但仍處於logical2或
$_POST['select']=='2'

這是我的代碼:

<form action='' methode='post'> 
    <select name='select'> 
    <option value='1'>example1</option> 
    <option value='2'>example2</option> 
    </select> 
</form> 
<?php 
if($_POST['select']=='1') 
{ 
echo"<a href='?page=edit&id=$r[id]'>Edit</a>"; 
} 
elseif($_POST['select']=='2') 
{ 
echo"<a href='?page=edit&id=$r[id]'>Edit</a>"; 
} 
else 
{ 
echo"please select"; 
} 
?> 
+0

你需要在$ r [id]'的周圍放置大括號('{}')。除此之外,我不知道你在問什麼。 – worldofjr 2014-09-30 02:59:32

+0

hy worldofir對不起,如果我的代碼不完整,因爲它只是例如,但我的問題是更像像頭('location:index.php')重定向;但如何直接索引,但仍在條件$ _POST ['select'] =='2' – badruz 2014-09-30 03:05:05

回答

0

接頭,如果有的話,必須輸出到的第一件事瀏覽器。這包括在開始<?php標記之前沒有任何空白區域。

如果您想對發送哪個頭進行比較,那麼您可以,但這應該是您獲得$_POST變量進行比較之後所做的第一件事。

另外,您需要在位置標題中使用絕對地址。

例如;

<?php 
if($_POST['select']=='1') 
{ 
    header('Location: http://yoursite.com/index.php'); 
} 
elseif($_POST['select']=='2') 
{ 
echo"<a href='?page=edit&id={$r[id]}'>Edit</a>"; 
} 
else 
{ 
echo"please select"; 
} 
?> 
<form action='edit.php' method='post'> 
    <select name='select'> 
    <option value='1'>example1</option> 
    <option value='2'>example2</option> 
    </select> 
</form> 

希望這會有所幫助!

+0

hy worldofjr感謝您的答案 – badruz 2014-09-30 03:43:23