//失敗返回
function error()
{
global $smarty;
// die(var_dump($_POST));
$message = [];
if (empty($_POST['tilte'])) {
$message[] = '標題必填';
}
if (empty($_POST['directions'])) {
$message[] = '描述必填';
}
if (empty($_POST['end'])) {
$message[] = '到期日必填';
}
$smarty->assign('title', '錯誤提示頁');
$smarty->assign('message', $message);
$smarty->display('templates/error.tpl');
exit();
}
<meta http-equiv="refresh" content="5; url=index.php">
5秒後可自動轉向。
<!DOCTYPE html>
<html lang="zh-TW">
<meta http-equiv="refresh" content="5; url=index.php">
<!-- 引入檔頭 -->
{include file="header.tpl"}
<body>
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading">錯誤提示</h4>
<ul>
{foreach from=$message key=key item=value}
<li>{$key+1}.{$value}</li>
{/foreach}
</ul>
</div>
</body>
</html>
function.php
// 判斷日期格式是否正確
function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d"))
{
$unixTime = strtotime($date);
if (!$unixTime) {
return false;
}
foreach ($formats as $format) {
if (date($format, $unixTime) == $date) {
return true;
}
}
return false;
}
function.php
中 error()
加入判斷日期格式的函數
if (!checkDateIsValid($_POST['end'])) {
$message[] = '到期日的日期格式需為西元 YYYY-mm-dd 或 YYYY/mm/dd';
}
//失敗返回
function error($message, $refresh = '')
{
global $smarty;
$smarty->assign('page_title', '錯誤提示頁');
$smarty->assign('message', $message);
$smarty->assign('refresh', $refresh);
$smarty->display('templates/error.tpl');
exit();
}
// 判斷日期格式是否正確
function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d"))
{
$unixTime = strtotime($date);
if (!$unixTime) {
return false;
}
foreach ($formats as $format) {
if (date($format, $unixTime) == $date) {
return true;
}
}
return false;
}
function check_error()
{
$message = [];
if (empty($_POST['title'])) {
$message[] = '標題必填';
}
if (empty($_POST['directions'])) {
$message[] = '描述必填';
}
if (empty($_POST['end'])) {
$message[] = '到期日必填';
}
if (empty($_POST['assign'])) {
$message[] = '至少指派一名';
}
if (!checkDateIsValid($_POST['end'])) {
$message[] = '到期日的日期格式需為西元 YYYY-mm-dd 或 YYYY/mm/dd';
}
if (!empty($message)) {
error($message, 1);
exit();
}
}
//新增清單
function add()
{
global $db;
check_error();
//以下省略
}