如何在php中使用多个复选框并显示所有选定的值

时间:2014-10-02 03:07:21

标签: php arrays forms post checkbox

我有两个不同的文件:index.php,其中包含以下代码:

<!DOCTYPE html PUBLIC>
<html>
<head>
<title></title>
</head>

<body>
<div id="container">
    <h2>Information</h2>
    <form action="displayResults.php" method="post">

    <fieldset>
        <legend>Name:</legend>
        <label>First Name</label>
        <input type="text" name="fname" value="" class="textbox"/>
        <br />

        <lable>Last Name:</lable>
        <input type="text" name="lname" value="" class="textbox"/>
        <br />

        <label>Email:</label>
        <input type="text" name="email" value="" class="textbox"/>
    </fieldset>

    <fieldset>
        <p>Major:</p>
        <input type="radio" name="major" value="Computer Science" />
        Computer Science<br />
        <input type="radio" name="major" value="Web Design and Development" />
        Web Development and Design<br />
        <input type="radio" name="major" value="Computer Internet Techinician" />
        Computer Internet Technician<br />
        <input type="radio" name="major" value="Computer Engineer" />
        Computer Engineer<br />

        <p>Places You Have Visited</p>
        <input type="checkbox" name="location[]" value="North America" />
        North America<br />
        <input type="checkbox" name="location[]" value="South America"  />
        South America<br />
        <input type="checkbox" name="location[]" value="Europe" />
        Europe<br />
        <input type="checkbox" name="location[]" value="Asia" />
        Asia<br />
        <input type="checkbox" name="location[]" value="Australia" />
        Australia<br />
        <input type="checkbox" name="location[]" value="Africa" />
        Africa<br />
        <input type="checkbox" name="location[]" value="Antartica" />
        Antartica<br />

        <p>Comments</p>
            <textarea name="comments" rows="4" cols="30"></textarea>

    </fieldset>
    <input type="submit" value="Submit" />
    </form>
</div>

</body>
</html>

接下来,提交时调用的文件是:displayResults.php,其中包含以下代码:

<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$location = $_POST['location'];

if (isset($_POST['major'])) {
    $major = $_POST['major'];
} else {
    $major = 'None'; 
}

//if (isset($_POST['location'])) {
//  $location = $_POST['location'];
//} else {
//  $location = 'None'
//}

$comments = $_POST['comments'];
$comments = htmlspecialchars($comments);
$comments = nl2br($comments, false);
?>
<!DOCTYPE html PUBLIC>
<html>
<head>
    <title></title>
</head>
<body>
    <div id="container">
        <h2>Information</h2>

        <label>First Name:</label>
        <span><?php echo htmlspecialchars($fname); ?></span>
        <br />
        <label>Last Name:</label>
        <span><?php echo htmlspecialchars($lname); ?></span>
        <br />
        <label>Email:</label>
        <span><?php echo htmlspecialchars($email); ?></span>
        <br />

        <label>Major:</label>
        <?php echo $major; ?></span>
        <br />

        <label>Locations:</label>
        <span><?php print_r($location); ?></span>
        <br />

        <label>Comments:</label>
        <span><?php echo $comments; ?></span>
        <br />

    </div>
</body>

选择多个复选框时,我只会让其中一个复选框返回。我不明白如何检索多个复选框的值。我看过并试过多种方法。也许我错了。我想它与循环我的数组有关。请帮忙。

0 个答案:

没有答案