我有一个表格,用户需要输入他们的全名,联系电话和最佳时间来致电的细节。我想验证我的网络表单上的这些字段,以便用户不能填写表单时不提交表单。我已经使用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。 –