【问题标题】:PHP Sticky FormsPHP 粘性表单
【发布时间】:2016-02-05 07:02:59
【问题描述】:

我一直在为 php 做一个项目,关于粘滞键。这很简单。但是我遇到了一些错误......我正在使用 CodeLobster。

谁能帮我找出我的错误? 找了2hrs了,试了debug,这里实在不知道怎么用。

非常感谢。任何帮助将不胜感激

这就是我得到的:

输出应该是这样的:

<html><head><title>Empty Fields</title>
<body><div align="center">
    <h2>Validating Input</h2>
    <?php
        $errors = array();

        if(isset($_POST['submit'])){
            validate_input();

            if(count($errors) != 0){
                display_form();
            }
            else{
                echo "<b>OK! Go ahead and Process the form!</b><br/>";
            }

        }
        else{
            display_form();
        }

        function validate_input(){
            global $errors;

            if($_POST['name'] == ""){
                $errors['name'] = "<font color='red'>***Your name?***</font>";
            }

            if($_POST['phone'] == ""){
                $errors['phone'] = "<font color='red'>***Your phone?</font>";
            }
        }

        function display_form(){
            global $errors;
    ?>
            <b>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                What is your name?<br/>
                <input type="text" name="name" value="<?php echo $_POST['name']; ?>" /><br/>
                <?php echo $errors['name']; ?><br/>
                What is your phone number?<br/>
                <input type="text" name="phone" value="<?php echo $_POST['phone']; ?>" /><br/>
                <?php echo $errors['phone']; ?><br/>
                <input type="reset" />
                <input type="submit" name="submit" /><br/>
            </form></b>
    <?php
        }
    ?>
    </div>
</body>
</html>

【问题讨论】:

  • 错误是什么?你能展示一些错误的图片吗?
  • 你需要添加isset条件,或者你想使用的空条件,问题是,页面显示通知那些不是错误。

标签: php html forms sticky


【解决方案1】:
Can you please check once this code:-

<html><head><title>Empty Fields</title>
<body><div align="center">
    <h2>Validating Input</h2>
    <?php
        $errors = array();

        if(isset($_POST['submit'])){
            validate_input();

            if(count($errors) != 0){
                display_form();
            }
            else{
                echo "<b>OK! Go ahead and Process the form!</b><br/>";
            }

        }
        else{
            display_form();
        }

        function validate_input(){
            global $errors;

            if($_POST['name'] == ""){
                $errors['name'] = "Your name?";
            }

            if($_POST['phone'] == ""){
                $errors['phone'] = "Your phone?";
            }
        }

        function display_form(){
            global $errors;
    ?>
            <b>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                What is your name?<br/>
                <input type="text" name="name" value="<?php if(isset($_POST['name'])){echo $_POST['name'];} ?>" /><br/>
                <?php if(isset($errors['name'])){echo $errors['name'];} ?><br/>
                What is your phone number?<br/>
                <input type="text" name="phone" value="<?php if(isset($_POST['name'])){$_POST['phone'];} ?>" /><br/>
                <?php if(isset($errors['phone'])){echo $errors['phone'];} ?><br/>
                <input type="reset" />
                <input type="submit" name="submit" /><br/>
            </form></b>
    <?php
        }
    ?>
    </div>
</body>
</html>

【讨论】:

    【解决方案2】:

    我尝试修改你的代码,有很多地方要修复。
    首先,您需要将 $_POST['name'] 和 $_POST['phone'] 保留在变量中,以便在每个函数中使用。 像这样,

    $name = (isset($_POST['name']) ? $_POST['name'] : '');
    $phone = (isset($_POST['phone']) ? $_POST['phone'] : '');
    

    您还需要将下面的代码添加到需要使用此变量的函数的第一行

    global $name;
    global $phone;
    

    在函数 display_form 中,您需要在 print(echo) 行之前检查 $errors['name'] 和 $errors['name'] 是否为空。

    if (isset($errors['name'])) echo $errors['name'];
    if (isset($errors['phone'])) echo $errors['phone'];
    

    所以,最后代码应该如下所示,我尝试了这段代码没有错误。

    <html>
    <head><title>Empty Fields</title>
    <body>
    <div align="center">
        <h2>Validating Input</h2>
        <?php
            $errors = array();
    
            $name = (isset($_POST['name']) ? $_POST['name'] : '');
            $phone = (isset($_POST['phone']) ? $_POST['phone'] : '');
    
            if(isset($_POST['submit']))
            {
                validate_input();
    
                if(count($errors) != 0)
                {
                    display_form();
                }
                else
                {
                    echo "<b>OK! Go ahead and Process the form!</b><br/>";
                }
    
            }
            else
            {
                display_form();
            }
    
            function validate_input(){
                global $errors;
                global $name;
                global $phone;
    
                if($name == '')
                {
                    $errors['name'] = "<font color='red'>***Your name?***</font>";
                }
    
                if($phone == '')
                {
                    $errors['phone'] = "<font color='red'>***Your phone?</font>";
                }
            }
    
            function display_form(){
                global $errors;
                global $name;
                global $phone;
        ?>
                <b>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    What is your name?<br/>
                    <input type="text" name="name" value="<?php echo $name; ?>" /><br/>
                    <?php if (isset($errors['name'])) echo $errors['name']; ?><br/>
                    What is your phone number?<br/>
                    <input type="text" name="phone" value="<?php echo $phone; ?>" /><br/>
                    <?php if (isset($errors['phone'])) echo $errors['phone']; ?><br/>
                    <input type="reset" />
                    <input type="submit" name="submit" /><br/>
                </form></b>
        <?php
            }
        ?>
        </div>
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      只需使用 isset($var);避免未定义的索引:PHP 上的 EROOR。

      echo isset($_POST['name']); echo isset($_POST['phone']);

      随心所欲。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-06
        • 1970-01-01
        • 2010-09-21
        • 2017-07-16
        相关资源
        最近更新 更多