【问题标题】:How do you check if textarea is empty你如何检查textarea是否为空
【发布时间】: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


【解决方案1】:

要检查是否输入了数据,你应该这样使用strlen。

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

修剪将删除所有张贴的前导和尾随空白字符。

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

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

【讨论】:

    【解决方案2】:

    你真的可以使用几种不同的方法

    $msg = $_POST['message'];
    
    if ($msg == "") { // Error }
    
    if (!$msg) { // Error }
    
    if (strlen($msg) == 0) { // Error }
    

    等等……

    您可能希望使用trim 删除空白,因为它们可以放入大量空格并且它可能仍然有效。

    另外,不回答您的问题,但不要使用ereg 使用preg_match - ereg 已被弃用,建议不要再使用。您还希望在插入输入(或为此进行任何查询)之前转义 (mysql_real_escape_string) 或使用准备好的语句(首选方法)。

    【讨论】:

    • 这些都适用于普通的文本字段,但文本区域不同。即使在字段中没有写入任何内容,它也会输出 2 的 strlen,因此正确的做法是先修剪()字段;这将使它的 strlen 为 0。此时,您的方法将正常工作。
    【解决方案3】:

    如何检查textarea 是否为

    如果 empty 表示它没有内容,则使用 strlen() 检查用户输入。

    如果您对空的定义仅包含空白内容,请先在用户输入上执行trim()

    你不想使用empty(),如果字符串是0,在这种情况下对PHP但不一定对你的应用程序。

    【讨论】:

    • @alex,感谢您的提示。我试过这个 if (!empty ( $_POST['testimonial']) && trim($_POST['testimonial'])!=='') { ... 效果很好。
    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2012-06-17
    • 2014-03-19
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多