我有一个表格,要求用户输入他们的全名,联系电话和最佳时间来拨打详细信息。我想在我的网络表单上验证这些字段,以便用户无法在没有填写这些字段的情况下提交表单。我使用了if(empty($...))
并回显了我的错误消息但是,当我尝试提交空白表单时,它不会显示任何错误消息。我怎么能解决这个问题?
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;
}
?>
答案 0 :(得分:0)
嗯,逻辑上有错误。您正在echo
然后header
。这意味着它显示消息并将它们返回到上一页的速度超过了它们可以看到消息的速度。这真的是你想要做的吗?
您可能应该将表单发布到自身,而只是echo
而不显示您希望显示错误的header
,或者实现javascript表单验证。
此外,您可以使用
<?
header('Location: /call-back-form.php?message=Error');
?>
然后在call-back-form.php用户
中<?
echo $_GET['message'];
?>
需要的地方