PHP - 如果子句声明满足条件时打开不同的页面

问题描述:

我是一名PHP新手,我有一个输入提交表单,用户将输入他们的电话号码,它将为他们打开各自的页面。PHP - 如果子句声明满足条件时打开不同的页面

我想用if语句编写一个php脚本语句,如果条件满足,它将根据输入的电话号码打开相应的页面。

例如,我想的,如果使得输入子句语句

如果为1,第1页SHLD被输入打开 或 如果如图2所示,第2页应该被打开 或 如果3被输入,第3页应该打开 或 如果输入4,则应打开页面4 和尽可能多的页面,以便最终 如果没有满足条件,则应打开默认页面。

我该如何解决这个问题?

谢谢先进。

Megamix

+0

也许你想在开关盒发布您迄今为止所尝试的内容,并且我们可以从那里给您提供建议。 – fubar

+0

欢迎来到Stack Overflow!没有看到[mcve]就很难提供帮助。你可以发布一个吗? – ItamarG3

在您的表单提交页面,您可以做一个重定向到相应的页面。

例:

<form action="submit.php" method="post"> 
    <input type="number" name="phone" /> 
</form> 

submit.php

<?php 
$phone_no = $_POST['phone']; 
header("Location: detail.php?number=".$phone_no); // or any page you want.. 
die(); 
?> 

如果你希望它是根据条件,那么你可以做:

<?php 
$phone_no = $_POST['phone']; 
$redirect_location = "default.php"; 
if($phone_no == "11111"){ 
    $redirect_location = "11111.php" 
}else if($phone_no == "22222"){{ 
    $redirect_location = "22222.php" 
} 

header("Location:".$redirect_location); 
die(); 
?> 

请考虑upvoting并接受回答是否帮助你。

+0

嗨NewProgrammer,感谢您的帮助,它不工作。我试过了,它给了HTTP ERROR 500,多数民众赞成,这个页面不工作 megamix21.com目前无法处理此请求。 –

+0

请upvote我的答案/接受它,如果它解决您的问题:) – NewProgrammer

+0

这可能是您的服务器的问题。您需要检查服务器的错误日志以解决导致该问题的原因。 – NewProgrammer

按我在PHP的经验,你可以如下图所示通过写类似PHP脚本实现这一目标:

<?php 

$id = $_POST['phone_number']; 
/* Assuming Phone_number is the field name in the form if not change it to the same name as given in your HTML form */ 

if($id == '12345678') 
header('Location of the page where you want to redirect'); 
else if ($id == '12345687') 
header('Location of the second page where you want to redirect'); 
else 
header(default.php'); 

?> 
+0

嗨Udit Agarwal,它的工作完美。只是我必须修改标题方向为header('Location:default.php'); 非常感谢您的帮助。我很感激。 –

您也可以尝试

<?php 
$phone_no = $_POST['phone']; 
switch($phone_no){ 
    case "123456789": 
     header("Location: www.site.org"); 
     break; 
    case "987654321": 
     header("Location: www.site2.org"); 
     break; 
    default: 
     header("Location: www.site3.org"); 
     break; 
} 
?>