PHP粘性表格

时间:2016-02-05 07:03:00

标签: php html forms sticky

我一直在做一个关于粘性密钥的php项目。这很直截了当。但是我收到了一些错误...我正在使用CodeLobster。

任何人都可以帮我找到我的错误吗? 我一直在寻找2小时,我尝试了调试,但我真的不知道如何在这里使用它。

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

这是我得到的: enter image description here

输出应为: enter image description here

<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>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

我尝试修改你的代码,有很多要点要修复 首先,您需要在变量中保留$ _POST [&#39; name&#39;]和$ _POST [&#39; phone&#39;],以便在每个功能中使用。 像这样,

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

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

global $name;
global $phone;

在功能display_form中,您需要检查$ errors [&#39; name&#39;]并且$ errors [&#39; name&#39;]在打印(回显)该行之前是否为空。

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>

答案 2 :(得分:0)

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

echo isset($ _ POST ['name']); echo isset($ _ POST ['phone']);

您需要的任何地方。