正在尝试回显2个单选按钮的输出


Trying to echo output from 2 radio buttons

我有一个带有if/else语句的表单,我得到了工作的第一部分。这是一个用于电影分级的单选按钮。如果没有字段为空,则打印信息。但我需要插入另一个回声,这取决于他们是否说要联系他们进行调查,我不知道该把代码放在哪里。见下文:

<fieldset><legend>MOVIE WATCHED AND RATING SURVEY:</legend>
    Please tell us your name: <input type="text" name="name" value="<?php echo         $name;?>">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>
    Please tell us your email address: <input type="text" name="email" value="<?   php echo $email;?>">
    <span class="error">* <?php echo $emailErr;?></span>
    <br><br>
    Name one movie you watched recently: <input type="text" name="movie" value="<?php echo $movie;?>">
    <span class="error">* <?php echo $movieErr;?></span>
    <p><label>Please rate the movie:</label></p>
    <form action="" method="post">
        <input type="radio" name="rating" value="Very Bad" checked> Very Bad<br>
        <input type="radio" name="rating" value="Bad"> Bad<br>
        <input type="radio" name="rating" value="Middle-of-the-Road"> Middle of-the-Road<br>
        <input type="radio" name="rating" value="Good"> Good<br>
        <input type="radio" name="rating" value="Very Good"> Very Good<br>
    </form>
    <p><label>Would you like to be contacted again for addtional surveys?</label></p>
    <form action="" method="post">
        <input type="radio" name="survey" value="yes" checked> Yes<br>
        <input type="radio" name="survey" value="no"> No<br>
    </form>
    <p align="center"><input type="submit" name="submit" value="Submit My     Information" /></p>
</fieldset>
</form>
<?php # Program 2 Handle form
// Print the submitted information:
if ( !empty($_POST['name']) && !empty($_POST['email']) &&    !empty($_POST['movie']) ) {
    echo "Thank you for completing the survey. Here's what you said";
    echo "<p>Your name is {$_POST['name']}";
    echo "<p>Your e-mail address is {$_POST['email']}";
    echo "<p>You recently watched {$_POST['movie']} and felt it was    {$_POST['rating']}!";
    echo "<p>Thank you for agreeing to be contacted for future surveys";
} else { // Missing form value.
    echo '<p>Please go back and fill out all required fields.</p>';
}
?>

这就是我迷失的地方。我需要说"谢谢你同意接受调查"或"我们尊重你不这样做的意愿",如果他们在单选按钮上输入是或否

首先,您的HTML需要一些工作。如果所有这些信息都应该一起提交,那么所有信息周围应该只有一个<form>标签,如下所示:

<form action="" method="post">
    <fieldset><legend>MOVIE WATCHED AND RATING SURVEY:</legend>
        <label for="name">Please tell us your name:</label>
        <input type="text" name="name" value="<?php echo $name ?>">
        <span class="error">* <?php echo $nameErr ?></span>
        <br><br>
        <label for="email">Please tell us your email address: </label>
        <input type="text" name="email" value="<?php echo $email ?>">
        <span class="error">* <?php echo $emailErr ?></span>
        <br><br>
        <label for="movie">Name one movie you watched recently: </label>
        <input type="text" name="movie" value="<?php echo $movie ?>">
        <span class="error">* <?php echo $movieErr ?></span>
        <p><label for="rating">Please rate the movie:</label></p>
        <input type="radio" name="rating" value="Very Bad" checked> Very Bad<br>
        <input type="radio" name="rating" value="Bad"> Bad<br>
        <input type="radio" name="rating" value="Middle-of-the-Road"> Middle of-the-Road<br>
        <input type="radio" name="rating" value="Good"> Good<br>
        <input type="radio" name="rating" value="Very Good"> Very Good<br>
        <p>
            <label for="survey">
                Would you like to be contacted again for addtional surveys?
            </label>
        </p>
        <input type="radio" name="survey" value="yes" checked> Yes<br>
        <input type="radio" name="survey" value="no"> No<br>
        <p align="center">
            <input type="submit" name="submit" value="Submit My Information" />
        </p>
    </fieldset>
</form>

然后在您的PHP中,添加对survey选择的检查将非常容易:

// Print the submitted information:
if ( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['movie']) ) {
    echo "Thank you for completing the survey. Here's what you said";
    echo "<p>Your name is $_POST[name]";
    echo "<p>Your e-mail address is $_POST[email]";
    echo "<p>You recently watched $_POST[movie] and felt it was $_POST[rating]!";
    // add another if here
    if ($_POST['survey'] == 'yes') {
        echo "<p>Thank you for agreeing to be contacted for future surveys";
    } elseif ($_POST['survey'] == 'no') {
        echo "<p>We respect your with not to be contacted for future surveys";
    }
} else { // Missing form value.
    echo '<p>Please go back and fill out all required fields.</p>';
}

根据你的问题,我认为你想要。

if(isset($_GET["submit"]))
{
if(isset($_GET["addtional_surveys"])){
    if($_GET["addtional_surveys"]=="Yes"){
        //show your additional survey form here
    }else{
        //movie extra details
    }
}else{
//your form code goes here <form action="" method="get">
}
}else{
     //your form code goes here <form action="" method="get">
}

?>

我认为您可以从使用HTML5中受益。HTML5会在表单提交时自动给你一个响应。您可以将"必需"放在输入的末尾。此外,你不需要再过滤例如电子邮件,HTML5可以快速做到这一点。你可以在W3schools.com上找到更多关于这方面的信息。在你的表单部分位置:

<input type="text" name="movie" size="40" maxlength="60" />
<select name="rating" size="3">
<option>Vælg</option>
<option value="0">Very Bad</option>
<option value="1">Bad</option>
<option value="2">Middle-of-the-Road</option>
<option value="3">Good</option>
<option value="4">Very Good</option>
</select>
<p><label>Would you like to be contacted again for addtional surveys?  </label></p>
<input type="radio" name="status" value="YES" checked="checked" />&nbsp;   
<input type="radio" name="status" value="NO" />

在接收php脚本的位置:

<?php # Program 2 Handle form
// Print the submitted information:
if ( !empty($_POST['name']) && !empty($_POST['email']) &&    !empty($_POST['movie']) ) {
    echo "Thank you for completing the survey. Here's what you said";
    echo "<p>Your name is {$_POST['name']}";
    echo "<p>Your e-mail address is {$_POST['email']}";
    echo "<p>You recently watched {$_POST['movie']} and felt it was    {$_POST['rating']}!";
    if ($_POST['status']=='YES') {
        // respondent would like you to come back
        echo "<p>Thank you for agreeing to be contacted for future surveys";
    }else{
        // respondent does not want to particcipate in future surveys
        echo "<p>We respect your wish not to be contacted in the future";
    }
    }else{ // Missing form value.
        echo '<p>Please go back and fill out all required fields.</p>';
    }

我还没有对此进行测试,但它应该有效。