【发布时间】:2011-11-27 12:26:41
【问题描述】:
我有一个包含三个文本输入和一个文本区域的表单。在处理表单的 php 页面上,我想先检查以确保用户在处理表单之前输入了某些内容(输入字段不为空)。我将在下面提供表单处理页面的框架。 *(我通过省略表单本身来节省空间和时间,但他的文本输入字段具有以下名称属性“名称”、“公司”、“电子邮件”,并且文本区域字段的名称属性是“消息”)我会尽我所能在代码 cmets 中更详细地解释代码。
<?php
// Turn on output buffering. Allows for headers to be called anywhere on script. See
// pg228 Ulman.
ob_start();
if (isset($_POST['submit'])){
// Initialize a session to keep tract of error and success messages:
session_start();
// Define a session variable that keeps tract of how many times user
// accesses page.
$_SESSION['views'] = 1;
// Connect to the database.
require('config/config.php');
//Check for errors.
//Check to make sure they entered their name.
if (!empty ( $_POST['name'])){
$a = TRUE;
}
else {
$a = FALSE;
//This variable will be echoed on the form if field is missing a value
$_SESSION['name'] = '*Please enter a valid name.';
}
//Check to make sure they entered their company name.
if (!empty ( $_POST['company'])) {
$b = TRUE;
}
else{
$b = FALSE;
//This variable will be echoed on the form if field is missing a value
$_SESSION['company'] = '*Please enter the name of an institution you are affiliated with.';
}
//Check to make sure email is valid.
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
$c = TRUE;
}
else {
$c = FALSE;
//This variable will be echoed on the form if field is missing a value or email format is wrong.
$_SESSION['email'] = '*Please enter a valid email address.';
}
//Check to make sure they entered their message.
if (!empty ( $_POST['message'])) {
$d = TRUE;
}
else {
$d = FALSE;
//This variable will be echoed on the form if field is missing a value
$_SESSION['message'] = '*Please enter your message.';
}
//If no errors
if (empty($_SESSION['name'] ) && empty($_SESSION['company'] ) && empty($_SESSION['email'] ) && empty($_SESSION['message'] ) ) {
//Insert data into database.
$query = " table... ";
$result = mysql_query($query) or die("Data could not be inserted into table because: " .mysql_error());
if (mysql_affected_rows() == 1) {
// Display success message
//This variable will be echoed on the form if everything is filled out correctly.
$_SESSION['sent'] = "Your email has been sent. We will get back to you shortly.";
// Display contact page (the page containing the form)
header("Location: contact_page.php");
exit();
}
else{
die(mysql_error());
}
//Display error messages.
}
else {
// if errors array is not empty
// Display page (page containing the form)
header("Location: contact_page.php");
exit();
}//End of if there are errors.
}//End of if submit.
?>
原来如此。一切正常,除了检查文本区域(名为“消息”)是否为空的部分不起作用。如果我在未填写任何字段的情况下单击提交按钮,则会为文本输入字段而不是文本区域字段打印相应的错误消息。如果我正确填写了所有字段并将“消息”字段留空,则表单将正确处理并将值放入数据库并打印成功消息。当我为“消息”文本区域字段输入值时也是如此。现在我尝试了一些东西。我将 textarea 更改为文本字段,当我将此字段留空时,$_SESSION['message'] 变量最终得到了回显。显然,问题出在 textarea 标记中的命名属性未正确处理这一事实?谁知道这里出了什么问题?
【问题讨论】:
-
在
if (!empty ( $_POST['company'])) {之前插入以下内容:var_dump($_POST['company']);并在此处发布。 -
@ngunsum:下次请花更多时间让您的问题更具可读性 - 特别是代码和您向他人提供信息的方式。我花了一些时间为你做这件事(使代码更具可读性)。
标签: php