如何在复选框中显示用户输入?

时间:2014-11-19 14:53:42

标签: php post

这是复选框的php代码:

<b>Interests:</b>   <input type="checkbox" name="interests" value="interests">Nature/Wildlife
                    <input type="checkbox" name="interests" value="interests">Arts/Museum
                    <input type="checkbox" name="interests" value="interests">Neighbourhoods

如何在下一页显示用户输入?仅显示用户已勾选的那些。

让它看起来像

兴趣:

  

自然/野生动物

     

艺术/博物馆

使用$ _post方法

显示完整代码:

<head>
    <link rel="stylesheet" href="styles.css">
    <style>
        #nav {
            text-align:left;
        }
    </style>
</head>

<body>

<div id="header">
<h1> <img src="images/footprints.jpg"> Singapore Footprints</h1>
</div>

<div id="nav">
<form action="visitorProfile.php" method="post">
<p>
<!-- Write codes to add a textbox for name -->
<b>Name:</b> <input type="text" name="name" size="20" maxlength="40"value=""/>

</p>


<p>
<!-- Write codes to add a textbox for userid -->
<b>Userid:</b> <input type="text" name="Userid" size="20"/>

</p>

<p>
<!-- Write codes to add a textbox for password -->
<b>Password:</b> <input type="password" name="pwd" size="20"/>

</p>

<p>
<!-- Write codes to add a textbox for contact number -->
<b>Contact Number:</b> <input type="text" name="contactno" size="20"/>

</p>

<p>
<!-- Write codes to add radio buttons for status, either local or tourist -->
<b>Status:</b>  <input type="radio" name="Status" value="local">Local
                <input type="radio" name="Status" value="tourist">Tourist</br>

</p>

<p>
<!-- Write codes to add checkboxes for interests -->
<b>Interests:</b>   <input type="checkbox" name="interests[]" value="Nature/Wildlife">Nature/Wildlife
                    <input type="checkbox" name="interests[]" value="Arts/Museum">Arts/Museum
                    <input type="checkbox" name="interests[]" value="Neighbourhoods">Neighbourhoods

</p>


<input type="submit" name="submit" value="Register me!" />
<input type="reset" value="Reset" />
</form>
</div>

<div id="footer">
Copyright &copy; School of IIT
</div>

</body>
</html>

链接到此:

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="styles.css">
    <style>
        #nav {
            text-align:centre;
        }
    </style>
</head>

<body>
<?php
    $name=$_POST['name'];
    $userid=$_POST['Userid'];
    $contactno=@$_POST['contactno'];
    $status=$_POST['Status'];

    ?>

    <div id="header">
        <h1> <img src="images/footprints.jpg"> Singapore Footprints</h1>    
    </div>

    <div id="nav">

        Thank you for registering with us.
<?php   
    echo "Thank you, <b>$name</b>.<br/>";
    echo '</p>';
    echo "<b>You have entered the following details:</b><br/>";
    echo "<b>Userid:</b> $userid <br/>";
    echo "<b>Contact Number:</b> $contactno <br/>";
    echo "<b>Status:</b> $status <br/>";
     $topics = $_POST['interests'];
  if(empty($topics)) 
  {
    echo("You didn't select anything");
  } 
  else
  {
    $N = count($topics);

    echo("You selected $N topic(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($topics[$i] . " ");
    }
  }

?>
    <br> <br>
        Please <a href="comments.html">Click Here</a> to continue.
    </div>

    <div id="footer">
        Copyright &copy; School of IIT
    </div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

如果使用方括号编写输入名称,则可以访问数组中的选定值:

<input type="checkbox" name="interests[]" value="Nature/Wildlife">Nature/Wildlife
<input type="checkbox" name="interests[]" value="Arts/Museum">Arts/Museum
<input type="checkbox" name="interests[]" value="Neighbourhoods">Neighbourhoods

如果您现在使用POST提交此表单,则在加载的页面上$_POST['interests']包含所选值

答案 1 :(得分:0)

这将是我的复选框的示例html代码:

&#13;
&#13;
    <form action="interests.php" method="post">
        <title>INTERESTS</title>
    <input type="checkbox" name="interests[]" value="Nature/Wildlife" />Nature/Wildlife<br />
    <input type="checkbox" name="interests[]" value="Arts/Museum" />Arts/Museum<br />

    <input type="submit" name="formSubmit" value="Submit" />
      </form>
&#13;
&#13;
&#13;

这将是我的兴趣.php代码:

  <?php
  $topics = $_POST['interests'];
  if(empty($topics)) 
  {
    echo("You didn't select anything");
  } 
  else
  {
    $N = count($topics);

    echo("You selected $N topic(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($topics[$i] . " ");
    }
  }
?>