使用数组形式,分配对象数组 [英] Form that uses array, assign object to array
问题描述
我需要创建一个表单,允许用户选择从0到7和显示所有一个号码,该脚本将输出在车厢发现给用户的对象,如果显示所有被拾取,然后它运行一个循环,显示所有8个目标与火车号。但我的code并没有真正运行。
(请忽略我的code。如果它没有任何意义,但我似乎并没有得到数组:(请指出错误,谢谢你这么这么这么多)
< HTML和GT;
<身体GT;
<表格名称=一条龙的方法=GETACTION =test.php的><选择>
<期权价值=0NAME =对象> 0℃; /选项>
<期权价值=1NAME =对象> 1 LT; /选项>
<期权价值=2NAME =对象> 2'; /选项>
<期权价值=3NAME =对象>第3版; /选项>
<期权价值=4NAME =对象> 4℃; /选项>
<期权价值=5NAME =对象> 5℃/选项>
<期权价值=6NAME =对象> 6≤/选项>
<期权价值=7NAME =对象> 7 LT; /选项>
<期权价值=8NAME =对象> 8示/选项>
<期权价值=所有NAME =所有>显示所有的< /选项>
< /选择><输入类型=提交名称=提交ID =提交值=提交大小=10>
< /表及GT;< PHP$列车[0] =铅笔;
$火车[1] =马卡龙;
$火车[2] =注意事项;
$火车[3] =书;
$列车[4] =橡皮;
$列车[5] =蛋糕;
$列车[6] =笔记本电脑;
$列车[7] =薄荷;
$火车[8] =杯;如果($ _GET [提交]){
$火车= $ _GET ['对象'];
回声< P>我还训练$<!/ P>中;
}为($ I = 0; $ I<的sizeof($列车); $ I ++){
回声<立GT; 。 $火车[$ i]。 < /李>中;
}?>< /身体GT;
< / HTML>
有在code一些错误
- 选择应该有name属性
- 需要得到一个基于所选的选项对象,并显示结果。
使用下面的code
<表格名称=一条龙的方法=GET><选择名称=对象>
<期权价值=0> 0℃; /选项>
......
<期权价值=所有>显示所有的< /选项>
< /选择><输入类型=提交名称=提交ID =提交值=提交大小=10>
< /表及GT;
PHP code
$火车[0] =铅笔;
....
$火车[8] =杯;如果($ _GET [提交]){
$对象= $ _GET ['对象'];
如果($对象=='所有'){
为($ I = 0; $ I<的sizeof($列车); $ I ++){
回声<立GT; 。 $火车[$ i]。 < /李>中;
}
}其他{
回声< P>我有$火车[$对象<!/ P>中;
}}
I need to create a form that allows the user to select a number from 0 to 7 and "show all" and the script will then output the object found in that train car to the user, or if show all is picked then it runs a loop that shows all 8 objects along with the train number. But my code doesn't really run. (Please ignore my code if it doesn't make any sense, but I don't seem to get arrays :( Please point out the mistakes and thank you so so so much)
<html>
<body>
<form name="train" method="GET" action="test.php">
<select>
<option value="0" name="object">0</option>
<option value="1" name="object">1</option>
<option value="2" name="object">2</option>
<option value="3" name="object">3</option>
<option value="4" name="object">4</option>
<option value="5" name="object">5</option>
<option value="6" name="object">6</option>
<option value="7" name="object">7</option>
<option value="8" name="object">8</option>
<option value="all" name="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] ="pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
$train = $_GET['object'];
echo "<p>I have $train!</p>";
}
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
?>
</body>
</html>
There are some mistakes in the code
- Select should have name attribute
- Need to get object and display results based on the selected option.
Use the below code
<form name="train" method="GET" >
<select name="object">
<option value="0">0</option>
......
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
PHP code
$train[0] ="pencil";
....
$train[8] = "cup";
if ($_GET['submit']) {
$object = $_GET['object'];
if($object == 'all'){
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
}else{
echo "<p>I have $train[$object]!</p>";
}
}
这篇关于使用数组形式,分配对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!