gpt4 book ai didi

php - 你如何检查textarea是否为空

转载 作者:行者123 更新时间:2023-12-03 23:05:57 25 4
gpt4 key购买 nike

我有一个包含三个文本输入和一个文本区域的表单。在处理表单的php页面上,我想首先检查以确保用户在处理表单之前输入了一些东西(输入字段不为空)。我将在下面提供表单处理页面的骨架。 *(我通过省略表单本身来节省空间和时间,但他的文本输入字段具有以下名称属性“名称”、“公司”、“电子邮件”,并且文本区域字段的名称属性是“消息”)我会尽我所能在代码注释中更详细地解释代码。

<?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 标签中的命名属性没有被正确处理?谁知道这里出了什么问题?

最佳答案

要检查是否输入了数据,您应该以这种方式使用 strlen。

if (!strlen(trim($_POST['textarea'])))

修剪将删除发布的任何前导和尾随空白字符。

并确保正确发送表单数据。

您可以通过插入 var_dump($_POST) 来确保在页面的开头,这样您就可以检查发布的所有内容。

关于php - 你如何检查textarea是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com