PHP 单选按钮


PHP radio button

好的,所以我试图在我的PHP代码中使用单选按钮。我用PHP写了一个Madlib,我想写一个单选按钮,当用户选择它时,它会改变故事等。快乐的结局或悲伤的结局是我迄今为止拥有的代码。任何帮助将不胜感激。

<html>
    <head>
        <title>James Nygaard's Mablib</title>
    </head>
    <body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>">
        <h1 style="font-family:cursive;">Create your Madlib below:</h1>
        <form action="" method="post">
            Enter the name of a boy:
            <input name="noun1" type="text" value="<?= $_POST['noun1'] ?>" /><br />
            Enter an Adjective that describes a person:
            <input name="adj1" type="text" value="<?= $_POST['adj1'] ?>" /><br />
            Enter the name of a man:
            <input name="noun2" type="text" value="<?= $_POST['noun2'] ?>" /><br />
            Enter an Adjective that describes a person:
            <input name="adj2" type="text" value="<?= $_POST['adj2'] ?>" /><br />
            Enter the name of a woman:
            <input name="noun3" type="text" value="<?= $_POST['noun3'] ?>" /><br />
            Enter your favorite animal:
            <input name="noun4" type="text" value="<?= $_POST['noun4'] ?>" /><br />
            Enter a name:
            <input name="noun5" type="text" value="<?= $_POST['noun5'] ?>" /><br />
            Enter the name of your favorite city:
            <input name="noun6" type="text" value="<?= $_POST['noun6'] ?>" /><br />
            Enter a feeling that ends in "ness":
            <input name="adj3" type="text" value="<?= $_POST['adj3'] ?>" /><br />
            Enter a Verb that ends in "ing":
            <input name="verb2" type="text" value="<?= $_POST['verb2'] ?>" /><br />
            Enter the name of a boy:
            <input name="noun7" type="text" value="<?= $_POST['noun7'] ?>" /><br />
            Enter the name of a girl:
            <input name="noun8" type="text" value="<?= $_POST['noun8'] ?>" /><br />
            <input type="submit" value="Click here or press enter to see your MadLib"  /><br />
        </form>
        <div style="color: #2F4F4F; font-family: cursive;">
            <?php
            $bg = "8FBC8F";
            $fg = "2F4F4F";
            if(isset($_POST['noun1'])) {
                echo "<h1> The adventures of {$_POST['noun1']}. </h1>";
                echo "This is where i will put the story. I already have the story but the code is    really long so I left it out of this question.";
                echo "<br> The end.";
            }
            ?>
        </div>
    </body>
</html> 

在这个小空间里不是特别清楚,但我试图遵循你的代码风格。

Select an option:
<label>
    <input name="some_option" type="radio" value="option_1" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_1') echo 'checked="checked" '; ?>/> Option 1
</label>
<label>
    <input name="some_option" type="radio" value="option_2" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_2') echo 'checked="checked" '; ?>/> Option 2
</label>

如果你运行该代码,你应该看到发生了什么。