我是PHP的新手,我正在努力实现以下目标。如果任何用户单击提交按钮,它将检查php文件,如果发现错误,则应该停止执行并显示错误消息或继续。
我目前的代码是:
<?php include('mobile_check.php'); ?>
<div class="tab-pane active" role="tabpanel" id="step1">
<div class="mobile-grids">
<div class="mobile-left text-center">
<img src="images/mobile.png" alt="" />
</div>
<div class="mobile-right">
<h4>Enter your mobile number</h4>
<label>+91</label><input type="text" class="mobile-text" name="req_number" value="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" required="">
</div>
<?php echo $mobileError; ?>
</div>
<ul class="list-inline pull-right">
<li><button type="button" name="mob_submit" class="mob-btn btn btn-primary next-step">Next</button></li>
</ul>
</div>
我的PHP代码是
<?php
if( isset($_POST['mob_submit']) ) {
if (empty($req_number)) {
$error = true;
$mobileError = "Please Enter Mobile No.";
} else if (!preg_match("/^[0-9 ]{10}+$/",$req_number)) {
$error = true;
$mobileError = "Please Reenter correct no.";
}
}
?>
答案 0 :(得分:1)
您拥有的1个选项是例外:
try {
if( isset($_POST['mob_submit']) ) {
if (empty($_POST['req_number'])) {
throw new exception("Please Enter Mobile No.");
} else if (!preg_match("/^[0-9 ]{10}+$/",$_POST['req_number'])) {
throw new exception("Please Reenter correct no.");
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
在try块中,如果抛出异常,它将立即跳转到catch块。在提供的示例中,我只是回显了异常消息。