首页 > 解决方案 > 无法显示php文件的结果,帮帮我

问题描述

我不知道为什么它不能显示结果。我已经在上面放了isset()函数,但是回显字符串没有出来。

<form method="POST">
    <input type="radio" name="colors" value="red" checked="true"> 
    <label id="r">Red</label>
    <input type="radio" name="colors" value="green">
    <label id="g">Green</label>

    <input type="button" name="add" value="Add">
    <input type="button" name="clear" value="Clear">
</form>


<?php
    if (isset($_POST['add'])) {
        if (isset($_POST['colors'])) {
            $colorVal = $_POST['colors'];
            echo "$colorVal";
        }
    }
?>

标签: phphtmlforms

解决方案


您的按钮的类型Add必须submit也是按钮必须Clearreset。顺便说一句,$POST['colors']需要$_POST['colors']

<form method="POST">
    <input type="radio" name="colors" value="red" checked="true"> 
    <label id="r">Red</label>
    <input type="radio" name="colors" value="green">
    <label id="g">Green</label>

    <input type="submit" name="add" value="Add">
    <input type="reset" name="clear" value="Clear">
</form>


<?php
if (isset($_POST['add'])) {
    if (isset($_POST['colors'])) {
        $colorVal = $_POST['colors'];
        echo "$colorVal";
    }
}
?>

更新:(重置按钮按预期工作)

<form method="POST">
    <input type="radio" name="colors" value="red" checked="true"> 
    <label id="r">Red</label>
    <input type="radio" name="colors" value="green">
    <label id="g">Green</label>

    <input type="submit" name="add" value="Add">
    <input type="reset" name="clear" value="Clear">
</form>


推荐阅读