使用PHP if语句生成隐藏的输入字段

时间:2014-09-12 04:04:58

标签: php html5

<?php include 'header.php'; ?>
    Please select which criteria you would like to search by:
        <form name="form" method="post">
    <input type="radio" name="criteria" value="Author">Author<br>
    <input type="radio" name="criteria" value="Title">Title<br>
    <input type="radio" name="criteria" value="Price">Price<br>
             <input type="Submit" name='submit' id="search-submit" value="Submit" /></p>
        </form>
        <?php
        $authorstatus = 'unchecked';
        $titlestatus = 'unchecked';
        $pricestatus = 'unchecked';

        if(isset($_POST['submit']))
        {
            $criteria = $_POST['criteria'];
            if($criteria == 'Author')
            {
                $authorstatus = 'checked';
            }
            if($criteria == 'Title')
            {
                $titlestatus = 'checked';
            }
            if($criteria == 'Price')
            {
                $pricestatus = 'checked';
            }
        }
        if($authorstatus == 'checked')
        {
            echo "<form name="form1" method="post">";
            echo "<input type='text' name='name' id='name' /><br />";
            echo "</form>";
        }
<?php include 'footer.php'; ?>

我正在尝试使用PHP根据用户选择按作者,标题或价格进行搜索来生成输入字段。现在,在PHP中使用html的3个echo线,程序只生成一个空白页面。有没有办法用PHP和HTML来做到这一点?

2 个答案:

答案 0 :(得分:0)

您必须正确回显HTML字符串。

echo "<form name="form1" method="post">";

当您对字符串和属性值使用双引号时,此行将中断字符串。将该行更改为

echo "<form name='form1' method='post'>";

答案 1 :(得分:0)

if(isset($_POST['submit']))
        {
            $criteria = $_POST['criteria'];
            if($criteria == 'Author')
            {
                $authorstatus = 'checked';
            }
            if($criteria == 'Title')
            {
                $titlestatus = 'checked';
            }
            if($criteria == 'Price')
            {
                $pricestatus = 'checked';
            }
        /*}*/ // <---  remove this
        if($authorstatus == 'checked')
        {
            echo "<form name='form1' method='post'>"; // <--- correct this 
            echo "<input type='text' name='name' id='name' /><br />";
            echo "</form>";
        }
        } // <-- It should be here..