<!DOCTYPE html>
<html>
<head>
<title>My Form</title>
<meta charset="UTF-8" />
</head>
<body>
<form method="get">
<div>Name
<input name="name" size="15" type="text" />
</div>
<select multiple="yes" name="colors[]">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
<option> 4 </option>
<option> 5 </option>
</select>
</form>
<?php
$number=10;
echo "hello";
if (isset($_GET["name"])){
echo " ".$_GET["name"];
echo "<br />";
}
echo count($_GET["colors"]), " ", $_GET["colors"], "<br />";
echo $_GET[$colors[0]]," is your first color.";
if(isset($_GET["$colors"])){
echo "You must like ";
for($colornum=0;$colornum<count($_GET["$colors"]);$colornum++){
echo $_GET[$colors[$colornum]]," ";
}
}
?>
</body>
</html>
为什么这不起作用?选择多个未在数组中正确输出,或者可能
有很多调试的东西对我的理解没有多大帮助。我收到未定义的索引和变量错误。这是选择选项 1-3 后的输出:
Name
hello jimbo1qaz
3 Array
Notice: Undefined variable: colors in E:\xampp\htdocs\myform.php on line 28
Notice: Undefined index: in E:\xampp\htdocs\myform.php on line 28 is your first color.
Notice: Undefined variable: colors in E:\xampp\htdocs\myform.php on line 29
//this should be $_GET['colors'][0]
echo $_GET[$colors[0]]," is your first color.";
//the , is wrong, you need to use . (point)
echo $_GET[$colors[0]]," is your first color.";
//Delete the $
if(isset($_GET["$colors"])){
//it's $_GET['colors']
for($colornum=0; $colornum<count($_GET["$colors"]); $colornum++){
//Here you need to use $_GET['colors'][$colornum]
echo $_GET[$colors[$colornum]]," ";
}
要输出数组,请使用 print_r() 而不是 echo:
print_r($_GET["colors"]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)