我一直在尝试使用PHP验证HTML表单,这当然已成功运行。问题然后我有点问题在所有验证完成后添加包含成功消息的包含文件的位置。 这是我的代码:
<?php
$status=$codeErr="";
if (isset($_GET['check'])) {
$number=$_GET['number'];
$code=$_GET['code'];
if($number!=""){
if(preg_match("/[0-9]/",$number) and strlen($number)=="11"){
$four=substr($number, 0,4);
if($four!="0706" and $four!="0813" and $four!="0803" and $four!="0806" and $four!="0703" and $four!="0816" and $four!="0810" and $four!="0814" and $four!="0903"){
$status='<p class="status">Number Entered is not an MTN Number</p>';
}
}
else{
$status='<p class="status">Invalid Number Format</p>';
}
}
else{
$status='<p class="status">Please Enter an MTN Number</p>';
}
if($code!=""){
if($code!=="324"){
$codeErr='<p class="status">Winning Code Not Recognized</p>';
}
}
else{
$codeErr='<p class="status">Please Enter Your Winning Code</p>';
}
}
如果未设置,此包含文件将显示该表单。
else if(!isset($_GET['check'])){
include('includes/check.php');
}
?>
现在我有另一个包含文件,其中包含在检查表单关闭时满足所有条件时要显示的消息。我在哪里包括它?
答案 0 :(得分:0)
您应该使用例外:
try {
if (!$somethingOne) {
throw new Exception('Something wrong one!');
}
if (!$somethingTwo) {
throw new Exception('Something wrong two!');
}
if (!$somethingMore) {
throw new Exception('Something wrong more!');
}
// success here only
echo 'All right!';
} catch (Exception $e) {
echo $e->getCode() . '<p>' . $e->getMessage() . '</p>';
}