PHP在表单提交后的<input />之后附加<span>

时间:2017-06-09 10:08:41

标签: php html

我必须用php构建一个计算器。当我按下提交按钮时,如果x的值不是数字,我必须在输入标签后显示跨度错误。

<form action="" method="GET" onsubmit="return keepCombobox();">
        <h1>Multiplicador</h1>
        <!--Draws the box around the related elements-->
        <fieldset>
            <p id="x">
                <!--If the label is pressed, sends the focus to the input-->
                <label for="x">X: </label>

                <input type="text" id="x" name="x" value="<?php echo html_entity_decode($x, ENT_COMPAT, "UTF-8"); ?>"/>
            </p>

            <p id="y">
                <!--If the label is pressed, sends the focus to the input-->
                <label for="y">Y: </label>
                <input type="text" id="y" name="y" value="<?php echo html_entity_decode($y, ENT_COMPAT, "UTF-8"); ?>"/><br><!--span style="color: red">valor nao valido</span-->
            </p>

            <p>
                <select name="op" required>
                    <option value=""<?php if ($op == "") { ?> selected<?php } ?>>-- Operação --</option>
                    <option value="+"<?php if ($op == "+") { ?> selected<?php } ?>>Soma</option>
                    <option value="-"<?php if ($op == "-") { ?> selected<?php } ?>>Subtração</option>
                    <option value="*"<?php if ($op == "*") { ?> selected<?php } ?>>Multiplicação</option>
                    <option value="/"<?php if ($op == "/") { ?> selected<?php } ?>>Divisão</option>
                </select>
            </p>

            <p>
                <!--Button-->
                <input type="submit" value="Calcular"/>
            </p>

            <?php
                if ($_GET){
            ?>

            <p>
                <?php
                if (!is_numeric($x)){
                    echo "<br><span style=\"color: red\">valor nao valido</span>";
                } else if (!is_numeric($y)) {
                    echo "<br><span style=\"color: red\">valor nao valido</span>";
                } else {
                    switch($op){
                    case '+':
                        echo "O resultado é  " . ($x + $y) ;
                        break;
                    case '-':
                        echo "O resultado é  " . ($x - $y) ;
                        break;
                    case '*':
                        echo "O resultado é  " . ($x * $y) ;
                        break;
                    case '/':
                        if ($y == 0) {
                            echo "Não é possível dividir por zero";
                        }
                        else {
                            echo "O resultado é  " . ($x / $y) ;
                        }
                        break;
                    }
                }
                ?>
            </p>
            <?php
                }
            ?>
        </fieldset>
    </form>

我想知道如何追加

br><span style="color: red">valor nao valido</span>

输入后我得到值

3 个答案:

答案 0 :(得分:0)

您可以更新代码:

<form action="" method="GET" onsubmit="return keepCombobox();">
    <h1>Multiplicador</h1>
    <!--Draws the box around the related elements-->
    <fieldset>
        <p id="x">
            <!--If the label is pressed, sends the focus to the input-->
            <label for="x">X: </label>

            <input type="number" id="x" name="x" value="<?php echo html_entity_decode($x, ENT_COMPAT, "UTF-8"); ?>"/>
            <?php 
            if (isset($x) && !is_numeric($x)) {
                ?><br><span style="color: red">valor nao valido</span><?php
            }
            ?>
        </p>

        <p id="y">
            <!--If the label is pressed, sends the focus to the input-->
            <label for="y">Y: </label>
            <input type="number" id="y" name="y" value="<?php echo html_entity_decode($y, ENT_COMPAT, "UTF-8"); ?>"/>

            <?php 
            if (isset($y) && !is_numeric($y)) {
                ?><br><span style="color: red">valor nao valido</span><?php
            }
            ?>
        </p>

        <p>
            <select name="op" required>
                <option value=""<?php if ($op == "") { ?> selected<?php } ?>>-- Operação --</option>
                <option value="+"<?php if ($op == "+") { ?> selected<?php } ?>>Soma</option>
                <option value="-"<?php if ($op == "-") { ?> selected<?php } ?>>Subtração</option>
                <option value="*"<?php if ($op == "*") { ?> selected<?php } ?>>Multiplicação</option>
                <option value="/"<?php if ($op == "/") { ?> selected<?php } ?>>Divisão</option>
            </select>
        </p>

        <p>
            <!--Button-->
            <input type="submit" value="Calcular"/>
        </p>
        <?php if (isset($x) && isset($y) && is_numeric($x) && is_numeric($y)): ?>
            <p><?php
                switch($op) {
                    case '+':
                        echo "O resultado é  " . ($x + $y) ;
                        break;
                    case '-':
                        echo "O resultado é  " . ($x - $y) ;
                        break;
                    case '*':
                        echo "O resultado é  " . ($x * $y) ;
                        break;
                    case '/':
                        if ($y == 0) {
                            echo "Não é possível dividir por zero";
                        }
                        else {
                            echo "O resultado é  " . ($x / $y) ;
                        }
                        break;
                    }
                }
            ?></p>
        <?php endif; ?>
    </fieldset>
</form>

我已将is_numeric测试移到输入字段下方,以显示您想要的范围。

然后我还更新了输入类型以使用number。此自定义类型将告诉浏览器该字段必须是数字,因此如果支持它将在提交表单之前应用一些验证。如果不受支持,它将作为简单的文本输入处理。

在脚本结束时我已经更新了你的测试,你不想依赖于$ _GET,因为这里唯一重要的是检查是否定义了$ x和$ y。

注意你的代码,似乎你使用的是register_globals,PHP PHP 5.4.0以后不再支持它。

您的代码无法在更新的PHP版本上执行。

您必须在使用之前定义类似的变量:

<?php
    $x = isset($_GET['x'])?$_GET['x']:0;
    $y = isset($_GET['y'])?$_GET['y']:0;
    $op = isset($_GET['op'])?$_GET['op']:'';
?>

答案 1 :(得分:0)

您需要在变量中分配GET值:

<?php
if ($_GET){
if(isset($_GET['x']) && $_GET['x'] != ''){$x = $_GET['x'];}else{$x = '';}
if(isset($_GET['y']) && $_GET['y'] != ''){$y = $_GET['y'];}else{$x = '';}
if(isset($_GET['op']) && $_GET['op'] != ''){$x = $_GET['op'];}else{$x = '';}
?>

答案 2 :(得分:0)

如果有人有同样的问题,这是一个简单的答案

<input type="text" id="y" name="y" value="<?php echo html_entity_decode($y, ENT_COMPAT, "UTF-8") ?>"/><?php if (array_key_exists("y", $_GET) && !is_numeric($y)) { ?><br><span style="color: red">valor nao valido</span><?php } ?>