【问题标题】:How to resolve the error in locahost xammp? arguments and their datatypes issue?如何解决 localhost xampp 中的错误?参数及其数据类型问题?
【发布时间】:2022-01-21 10:38:22
【问题描述】:

致命错误:

Uncaught TypeError: trim(): Argument #1 ($string) must be of type string, array given in C:\xampp\htdocs\php\required_valid_form.php:52 Stack trace: #0 C:\xampp\ htdocs\php\required_valid_form.php(52): trim(Array) #1 C:\xampp\htdocs\php\required_valid_form.php(24): test_input(Array) #2 {main} 在 C:\xampp\htdocs 中抛出\php\required_valid_form.php 第 52 行

我的代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .error {
            color:#FF0000;
        }
    </style>
</head>
<body>
    <?php
        $name=$email=$website=$comment=$gender="";
        $nameErr=$emailErr=$genderErr="";
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (empty($_POST["name"])) {
                $nameErr = "Name is Required";
            } else {
                $name = test_input($_POST=["name"]);
            }
            if (empty($_POST["email"])) {
                $emailErr = "Your Email is Required";
            } else {
                $email = test_input($_POST=["email"]);
            }
            if(empty($_POST["website"])) {
                $website = "";
            } else {
                $website = test_input($_POST=["website"]);
            }
            if (empty($_POST["comment"])) {
                $comment = "";
            } else {
                $comment = test_input($_POST=["comment"]);
            }
            if (empty($_POST["gender"])) {
                $genderErr = "It is required!";
            } else {
                $gender = test_input($_POST=["gender"]);
            }
        }
        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }    
    ?>
    <p><span class="error">Required Field *</span> </p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        Name : <input type="text" name="name"><span class="error">* <?php echo $nameErr; ?></span>
        <br><br>
        Email : <input type="email" name="email"><span class="error">* <?php echo $emailErr; ?> </span>
        <br><br>
        Website: <input type="text" name="website" id=""><br><br>
        Comment: <textarea name="comment" id="" cols="30" rows="10"></textarea><br>
        Gender :  <input type="radio" name="gender" id="">Male
                  <input type="radio" name="gender" id="">Female
                  <input type="radio" name="gender" id="">Other
                  <span class="error">
                      *<?php echo $genderErr; ?>
                  </span>
                  <br><br>
                  <input type="submit" value="Submit Here">
              </form>
          <?php
              echo "<h2>Your Input</h2>";
              echo $name;
              echo "<br><br>";
              echo $email;
              echo "<br><br>";
              echo $website;
              echo "<br><br>";
              echo $comment;
              echo "<br><br>";
              echo $gender;
          ?>
</body>
</html>

【问题讨论】:

  • 你想用$_POST=["name"] 结构完成什么?你正在用它破坏输入数据。 =assignment operator
  • 如何找出在哪里使用赋值运算或其他运算符?你有什么要推荐给我的吗?
  • 阅读手册并阅读一些 PHP 101 教程。一般来说,如果你想设置一个变量,使用=,如果你想读取一个变量,不要使用=。这是 PHP(以及大多数其他语言)的基础知识。
  • PHP 101 教程?他们在哪里?

标签: php arrays xampp arguments


【解决方案1】:

问题:$_POST=["name"]我在这里使用了不正确的赋值运算符。

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    <style>
        .error {
            color:#FF0000;
        }
    </style>
    </head>
       
    <body>
    <?php
            $name=$email=$website=$comment=$gender="";
            $nameErr=$emailErr=$genderErr="";
            if($_SERVER["REQUEST_METHOD"] == "POST") {
    
                if (empty($_POST["name"])) {
                    $nameErr = "Name is Required";
                } else {
                    $name = test_input($_POST["name"]);
                }
                if (empty($_POST["email"])) {
                    $emailErr = "Your Email is Required";
                }
                    else {
                    $email = test_input($_POST["email"]);
                    }
                if(empty($_POST["website"])) {
                    $website="";
                }
                else {
                    $website = test_input($_POST["website"]);
                }
                if (empty($_POST["comment"])) {
                    $comment="";
                }
                else {
                    $comment = test_input($_POST["comment"]);
                }
                if(empty($_POST["gender"])) {
                    $genderErr = "It is required!";
                }
                else {
                    $gender = test_input($_POST["gender"]);
                }
            }
            function test_input($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }    
    ?>
        <p><span class="error">Required Field *</span> </p>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
            Name : <input type="text" name="name"><span class="error">* <?php echo $nameErr; ?></span>
            <br><br>
            Email : <input type="email" name="email"><span class="error">* <?php echo $emailErr; ?> </span>
            <br><br>
            Website: <input type="text" name="website" id=""><br><br>
            Comment: <textarea name="comment" id="" cols="30" rows="10"></textarea><br>
            Gender :  <input type="radio" name="gender" value="Male">Male
                        <input type="radio" name="gender" value="Female">Female
                        <input type="radio" name="gender" value="Other">Other <span class="error">*<?php echo $genderErr; ?></span>
                        <br><br>
            <input type="submit" value="Submit Here">
        </form>
        <?php
        echo "<h2>Your Input</h2>";
        echo $name;
        echo "<br><br>";
        echo $email;
        echo "<br><br>";
        echo $website;
        echo "<br><br>";
        echo $comment;
        echo "<br><br>";
        echo $gender;
        
        ?>
    
    </body>
    </html>

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2019-11-25
    相关资源
    最近更新 更多