PHP验证表格

发布于 2025-02-12 21:24:19 字数 3399 浏览 1 评论 0原文

我很难显示我在PHP教程中拨打printf中的输出。我刚刚遵循了本教程,但仍然无法弄清楚在Localhost中显示printf内部的变量有什么问题。有人可以帮助我吗?谢谢。

    <?php
            $name = '';
            $password = '';
            $gender = '';
            $color = '';
            $languages = [];
            $comments = '';
            $tc = '';
        
           if (isset($_POST['submit'])) {
                if (isset($_POST['name'])) {
                    $name = $_POST['name'];
                };
                if (isset($_POST['password'])) {  
                    $password = $_POST['password'];
                };
                if (isset($_POST['gender'])) {
                    $gender = $_POST['gender'];
                };
                if (isset($_POST['color'])) {
                    $color = $_POST['color'];
                };
                if (isset($_POST['languages'])) {
                    $languages = $_POST['languages'];
                };
                if (isset($_POST['comments'])) {
                    $comments = $_POST['comments'];
                };
                if (isset($_POST['tc'])) {
                    $tc = $_POST['tc'];
                };
            //here's the problem i cant resolve printing out the output//
            printf('User name: %s
                <br>Password: %s
                <br>Gender: %s
                <br>Color: %s
                <br>Language(s): %s
                <br>Comments: %s
                <br>T&amp;C: %s',
                htmlspecialchars($name, ENT_QUOTES),
                htmlspecialchars($password, ENT_QUOTES),
                htmlspecialchars($gender, ENT_QUOTES),
                htmlspecialchars($color, ENT_QUOTES),
                htmlspecialchars(implode('', $languages), ENT_QUOTES),
                htmlspecialchars($comments, ENT_QUOTES),
                htmlspecialchars($tc, ENT_QUOTES));
        
                
            }
        ?>
        
        
        <form action="" 
            method="post">
              User name:  <input type="text" name="name"><br>
              Password:  <input type="password" value="password"><br>
              Gender:
              <input type="radio" name="gender" value="f"> female
              <input type="radio" name="gender" value="m"> male
              <input type="radio" name="gender" value="o"> other<br/>
              Favorite color:
              <select name="color">
                <option value="">Please select</option>
                <option value="#f00">red</option>
                <option value="#0f0">green</option>
                <option value="#00f">blue</option>
        </select><br>
            Languages spoken:
            <select name="languages[]"multiple size="3">
                <option value="en">English</option>
                <option value="fr">French</option>
                <option value="it">Italian</option>
                </select><br>
                Comments: <textarea name="comments"></textarea><br>
            <input type="checkbox" name="tc" value="ok"> I accept the T&amp;C<br> 
        <input type="submit" value="Register">
        
        </form>`

I am having trouble showing the output that i called in printf in php tutorial. I just followed the tutorial but still can't figure out what is wrong in displaying the variables inside the printf in localhost. Is anyone can help me. Thank you.

    <?php
            $name = '';
            $password = '';
            $gender = '';
            $color = '';
            $languages = [];
            $comments = '';
            $tc = '';
        
           if (isset($_POST['submit'])) {
                if (isset($_POST['name'])) {
                    $name = $_POST['name'];
                };
                if (isset($_POST['password'])) {  
                    $password = $_POST['password'];
                };
                if (isset($_POST['gender'])) {
                    $gender = $_POST['gender'];
                };
                if (isset($_POST['color'])) {
                    $color = $_POST['color'];
                };
                if (isset($_POST['languages'])) {
                    $languages = $_POST['languages'];
                };
                if (isset($_POST['comments'])) {
                    $comments = $_POST['comments'];
                };
                if (isset($_POST['tc'])) {
                    $tc = $_POST['tc'];
                };
            //here's the problem i cant resolve printing out the output//
            printf('User name: %s
                <br>Password: %s
                <br>Gender: %s
                <br>Color: %s
                <br>Language(s): %s
                <br>Comments: %s
                <br>T&C: %s',
                htmlspecialchars($name, ENT_QUOTES),
                htmlspecialchars($password, ENT_QUOTES),
                htmlspecialchars($gender, ENT_QUOTES),
                htmlspecialchars($color, ENT_QUOTES),
                htmlspecialchars(implode('', $languages), ENT_QUOTES),
                htmlspecialchars($comments, ENT_QUOTES),
                htmlspecialchars($tc, ENT_QUOTES));
        
                
            }
        ?>
        
        
        <form action="" 
            method="post">
              User name:  <input type="text" name="name"><br>
              Password:  <input type="password" value="password"><br>
              Gender:
              <input type="radio" name="gender" value="f"> female
              <input type="radio" name="gender" value="m"> male
              <input type="radio" name="gender" value="o"> other<br/>
              Favorite color:
              <select name="color">
                <option value="">Please select</option>
                <option value="#f00">red</option>
                <option value="#0f0">green</option>
                <option value="#00f">blue</option>
        </select><br>
            Languages spoken:
            <select name="languages[]"multiple size="3">
                <option value="en">English</option>
                <option value="fr">French</option>
                <option value="it">Italian</option>
                </select><br>
                Comments: <textarea name="comments"></textarea><br>
            <input type="checkbox" name="tc" value="ok"> I accept the T&C<br> 
        <input type="submit" value="Register">
        
        </form>`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三月梨花 2025-02-19 21:24:19

此处的问题是如果(ISSET($ _ post ['submit']))没有任何表格字段,即name submit,并且执行永远不会成立。 删除提交条件的if条件,否则将Sumbit按钮名称称为提交

<input type="submit" value="Register" name="submit">

The problem here is if (isset($_POST['submit'])) there is no any form field with the name submit and it will never become true to execute. Remove the if Condition with submit or Else give the sumbit button name as Submit

<input type="submit" value="Register" name="submit">
々眼睛长脚气 2025-02-19 21:24:19

您以前的代码中的错误是:

  1. 如果语句结束之后,则不需要semicolons ;
  2. 没有名称属性为密码以表单中的输入指定。
  3. 将表单数据的形式数据称为如果(isset($ _ post ['submit'])){...}而无需在表单中指定name =“ submit”属性。
  4. 爆破函数的使用无效,它将生成输出就像enfrit,反之亦然。

其他提示

  1. 必需的属性添加到每个输入以防止发送空形式数据。
  2. 您已经在表单标签中指定method =“ post”,因此,无需验证每个输入字段为if(isset($ _ post ['fieldName']))){... }如果(ISSET($ _ post ['submit'])){...}将以post方法发送所有表单数据。

最后,这是您的更新代码。

<?php

if (isset($_POST['submit'])) {
    $name = '';
    $password = '';
    $gender = '';
    $color = '';
    $languages = [];
    $comments = '';
    $tc = '';

    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    }
    if (isset($_POST['password'])) {
        $password = $_POST['password'];
    }
    if (isset($_POST['gender'])) {
        $gender = $_POST['gender'];
    }
    if (isset($_POST['color'])) {
        $color = $_POST['color'];
    }
    if (isset($_POST['languages'])) {
        $languages = $_POST['languages'];
    }
    if (isset($_POST['comments'])) {
        $comments = $_POST['comments'];
    }
    if (isset($_POST['tc'])) {
        $tc = $_POST['tc'];
    }

    //print output
    printf('User name: %s
    <br>Password: %s
    <br>Gender: %s
    <br>Color: %s
    <br>Language(s): %s
    <br>Comments: %s
    <br>T & C: %s
    <br><br>',
    htmlspecialchars($name, ENT_QUOTES),
    htmlspecialchars($password, ENT_QUOTES),
    htmlspecialchars($gender, ENT_QUOTES),
    htmlspecialchars($color, ENT_QUOTES),
    htmlspecialchars(implode(', ', $languages), ENT_QUOTES),
    htmlspecialchars($comments, ENT_QUOTES),
    htmlspecialchars($tc, ENT_QUOTES));
}

?>

<form action="" method="post">
    User name: <input type="text" name="name">
    <br>
    Password: <input type="password" name="password">
    <br>
    Gender:
    <input type="radio" name="gender" value="f"> female
    <input type="radio" name="gender" value="m"> male
    <input type="radio" name="gender" value="o"> other
    <br>
    Favorite color:
    <select name="color">
        <option value="">Please select</option>
        <option value="#f00">red</option>
        <option value="#0f0">green</option>
        <option value="#00f">blue</option>
    </select>
    <br>
    Languages spoken:
    <select name="languages[]" multiple size="3">
        <option value="en">English</option>
        <option value="fr">French</option>
        <option value="it">Italian</option>
    </select>
    <br>
    Comments: <textarea name="comments"></textarea>
    <br>
    <input type="checkbox" name="tc" value="ok"> I accept the T & C
    <br>
    <input type="submit" name="submit" value="Register">
</form>

Errors in your previous code are:

  1. No semicolons ; are needed after ending if statements.
  2. No name attribute is specified for password input in the form.
  3. Posting form data as if (isset($_POST['submit'])) {...} without specifying the name="submit" attribute in the form.
  4. Invalid use of implode function which will generate output be like enfrit or vice versa.

Additional Tips

  1. Add the required attribute to each input to prevent from sending empty form data.
  2. You have already specified method="post" in the form tag so, no need for validating each input field as if (isset($_POST['fieldname'])) {...} . if (isset($_POST['submit'])) {...} will send all the form data as POST method.

And lastly, here is your updated code.

<?php

if (isset($_POST['submit'])) {
    $name = '';
    $password = '';
    $gender = '';
    $color = '';
    $languages = [];
    $comments = '';
    $tc = '';

    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    }
    if (isset($_POST['password'])) {
        $password = $_POST['password'];
    }
    if (isset($_POST['gender'])) {
        $gender = $_POST['gender'];
    }
    if (isset($_POST['color'])) {
        $color = $_POST['color'];
    }
    if (isset($_POST['languages'])) {
        $languages = $_POST['languages'];
    }
    if (isset($_POST['comments'])) {
        $comments = $_POST['comments'];
    }
    if (isset($_POST['tc'])) {
        $tc = $_POST['tc'];
    }

    //print output
    printf('User name: %s
    <br>Password: %s
    <br>Gender: %s
    <br>Color: %s
    <br>Language(s): %s
    <br>Comments: %s
    <br>T & C: %s
    <br><br>',
    htmlspecialchars($name, ENT_QUOTES),
    htmlspecialchars($password, ENT_QUOTES),
    htmlspecialchars($gender, ENT_QUOTES),
    htmlspecialchars($color, ENT_QUOTES),
    htmlspecialchars(implode(', ', $languages), ENT_QUOTES),
    htmlspecialchars($comments, ENT_QUOTES),
    htmlspecialchars($tc, ENT_QUOTES));
}

?>

<form action="" method="post">
    User name: <input type="text" name="name">
    <br>
    Password: <input type="password" name="password">
    <br>
    Gender:
    <input type="radio" name="gender" value="f"> female
    <input type="radio" name="gender" value="m"> male
    <input type="radio" name="gender" value="o"> other
    <br>
    Favorite color:
    <select name="color">
        <option value="">Please select</option>
        <option value="#f00">red</option>
        <option value="#0f0">green</option>
        <option value="#00f">blue</option>
    </select>
    <br>
    Languages spoken:
    <select name="languages[]" multiple size="3">
        <option value="en">English</option>
        <option value="fr">French</option>
        <option value="it">Italian</option>
    </select>
    <br>
    Comments: <textarea name="comments"></textarea>
    <br>
    <input type="checkbox" name="tc" value="ok"> I accept the T & C
    <br>
    <input type="submit" name="submit" value="Register">
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文