我有一個表格,用戶需要輸入他們的全名,聯繫電話和最佳時間來致電的細節。我想驗證我的網絡表單上的這些字段,以便用戶不能填寫表單時不提交表單。我已經使用if(empty($...))
並回應了我的錯誤消息,但是,當我嘗試提交空白表單時,它不顯示任何錯誤消息。我怎麼解決這個問題?php錯誤消息不顯示在空白字段
CODE
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/inc/bootstrap.php');
include("config/cn.php");
$template['template'] = "page";
if($_POST)
{
// Data pulled from input form
$full_name = $_POST['full_name'];
if (empty($_POST['full_name']))
{
// Set error and return to form
echo "Field cannot be left blank";
header('Location: /call-back-form.php');
exit;
}
$contact_number = $_POST['contact_number'];
if (empty($_POST['contact_number']))
{
// Set error and return to form
echo "Field cannot be left blank";
header('Location: /call-back-form.php');
exit;
}
$best_time_to_call = $_POST['best_time_to_call'];
if (empty($_POST['best_time_to_call']))
{
// Set error and return to form
echo "Field cannot be left blank";
header('Location: /call-back-form.php');
exit;
}
$enter_sql = "INSERT INTO contact (full_name, contact_number, best_time_to_call)
VALUES('$full_name' , '$contact_number' , '$best_time_to_call')";
/*print($enter_sql);*/
$enter_query = mysql_query($enter_sql) or die(mysql_error());
header('Location: /thankyou.php');
exit;
}
?>
任何你'echo'之前調用'頭() '會丟失,說實話你不應該在'header()'之前'回顯'文件上的任何內容,因爲它會拋出'頭文件已經被傳遞'的錯誤 –
你不能阻止一個表單被提交一個後端la nguage。 –