<html>
<body>
<form method=post action="form_test.php">
<b>输入:</b>
姓名:<input type=text name="user_name">
密码:<input type=password name="user_password">
<input type=hidden name="user_id" value="25">
<br><br>
<b>单选按纽:</b>
<input type=radio name="color" value="red">红色
<input type=radio name="color" value="green">绿色
<input type=radio name="color" value="blue">蓝色
<br><br>
<b>多选按纽:</b>
<input type=checkbox name="style[]" value="b">粗体
<input type=checkbox name="style[]" value="u">下划线
<input type=checkbox name="style[]" value="I">斜体
<br><br>
<b>按纽:</b>
<input type=submit name="btn_ok" value="确定">
<input type=reset name="btn_cancel" value="重写">
</form>
<?php
//处理表单的函数
  function process_form(){
   //获取多选按纽的值
   foreach($_POST["style"] as $item){
$style_message1.="<$item>";
$style_message2.="</$item>";
}
echo "
$style_message1<font color=$_POST[color] style=$style_message>
你好,$_POST[user_name],密码为:$_POST[user_password],(id:$_POST[user_id])。
</font>$style_message2
";
}
//脚本在接受到btn_ok提交信息时执行处理函数
if($_POST["btn_ok"])
process_form();
?>
</body>
</html>
请问$style_message1<font color=$_POST[color] style=$style_message>
你好,$_POST[user_name],密码为:$_POST[user_password],(id:$_POST[user_id])。
</font>$style_message2
里面的$stle_message1和$style_message2有什么意义啊?

解决方案 »

  1.   

    <input type=checkbox name="style[]" value="b">粗体
    <input type=checkbox name="style[]" value="u">下划线
    <input type=checkbox name="style[]" value="I">斜体通过post传递给提交页面一个数组,数组名是$style,组内单项的值可能是"b","u"或"i",试用户选择而定。foreach($_POST["style"] as $item){
    $style_message1.="<$item>";
    $style_message2.="</$item>";
    }提交后php代码部分将得到的$_POST["style"]数组单项内容挨个复制到$item变量,而$style_message1和$style_message2则分别构成了字体修饰的开标签和闭标签。比如用户选择了"b"和"u",那么process_form()将执行两次循环:
    第一次后,$style_message1变成<b>,$style_message2变成</b>
    第二次后,因为.=的缘故,$style_message1后面又添加了<u>变成<b><u>,同理$style_message2则变成</b></u>。
    最后这两组开/关标签被echo输出到页面,一个在文字前打开标签,一个在文字后关闭标签。