当字段为空时如何阻止我的 php 页面继续 [英] how to stop my php page from continuing when field is empty

查看:38
本文介绍了当字段为空时如何阻止我的 php 页面继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php 
if(isset($_POST['submit'])){ 
echo "wht the heck"; 
$avail = new CheckEmptyFields(); 
$avail->availtime=$_POST['available_time']; 
echo $avail->chkFieldAvailableTime(); 
} 
else { 
print "<font color='red'>*</font>Printing else"; 
} 
?>

在我的页面中,但是当我按继续时,页面会在字段为空时进入下一页.

in my page but when i press continue the page goes on next page while field is empty.

因为我在页面上看到打印其他",即使我按了 b4 继续,所以脚本之前运行,而当用户按下继续按钮时它应该运行.

beacuse i see "printing else" on the page even b4 i press continue so the script ran before while it should run when the user presses continue button.

推荐答案

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (isset($_POST['submit'])) {
      ... yada yada yada ...
   } else {
      echo '<font yada yada yada';
   }
}

有了这个,如果页面是通过 POST 命中实际请求的,则只会检查 isset.如果它是通过 GET 或任何其他方法加载的,那段代码根本不会执行.

With this, the isset will ONLY be checked if the page was actually requested via a POST hit. If it's loaded via GET or any other method, that chunk of code simply won't execute at all.

这篇关于当字段为空时如何阻止我的 php 页面继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆