[英]Change php header using switch case
我想根據組合框中選擇的字符串重定向到其他頁面。
我添加了以下代碼:
switch ($downloadType){
case "text1":
header("Location:pdf/text1.pdf");
break;
case "text2":
header("Location:pdf/text2.pdf");
case "text3":
header("Location:pdf/text3.pdf");
default:
header("Location:index.html");
}
但是這個簡單的腳本不起作用。 我是php新手。 您知道為什么我無法更改標題嗎? 難道我們無法使用switch-case語句更改標頭。
如果不是這樣,那么重定向到表單提交上的另一頁的可能方法是什么。
您需要添加休息時間; 到每種情況的結尾。
case "text3":
header("Location:pdf/text3.pdf");
break;
您需要添加中斷:
switch ($downloadType){
case "text1":
header("Location:pdf/text1.pdf");
break;
case "text2":
header("Location:pdf/text2.pdf");
break;
case "text3":
header("Location:pdf/text3.pdf");
break;
default:
header("Location:index.html");
}
您必須將switch語句視為一種構造跳轉 。 根據執行的表達式的值,代碼執行將跳到第一個適當的case
或default
語句,然后從那里繼續。 這就是為什么您通常會想插入break
語句的原因。 當前,僅針對"text1"
情況進行此操作。
除此之外,我建議您在編寫Location
HTTP標頭時使用完整URL,即使相對URL通常可以按預期工作。 HTTP規范實際上並不允許相對URL。
header('Location: http://example.com/foo/pdf/text2.pdf');
並且不要忘記發送HTTP標頭不會停止腳本執行。 添加exit;
完成后的聲明。
除了使用break來完成case語句外,還請記住,必須在發送任何實際輸出之前調用header()。
例如:
<html>
<?php
header("Location:pdf/text2.pdf");?>
</html>
將不起作用,因為“ <html>
”行已經被發送到客戶端,並且不能使用header()。 基本上,在嘗試調用header()之前,請確保文件中沒有任何輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.