提问人:Lucy Walsh 提问时间:3/16/2015 最后编辑:Jay BlanchardLucy Walsh 更新时间:3/16/2015 访问量:87
几乎相同的 HTML 表单,一个发布正确的值,另一个则没有
Near identical HTML forms, one posts correct value, the other doesn't
问:
我的页面welcome.php上的两个表单如下所示:
<form action="welcomeforms.php" method="post" name="Food_Calories">
<h4>.</h4>//spacer
<input type="text" name="breakfast_calories" value="Calorie Amount" onFocus="this.value=''"><br>
<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">
</form>
<form action="welcomeforms.php" method="post" name="Foods">
<h4>Breakfast</h4>
<input type="text" name="breakfast" value="Add new breakfast item" onFocus="this.value=''"><br>
<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">
</form>
这是welcomeforms.php上的代码:
if(isset($_POST['breakfast'])){$breakfastitem = $_POST['breakfast'];}
if(isset($_POST['breakfast_calories'])){$breakfastcals = $_POST['breakfast_calories'];}
echo $breakfastitem;
echo $breakfastcals;
表单在网页上正确显示,当我在早餐表单中输入值“bread”,在早餐表单中输入“100”时,这是网页上返回的内容:
Undefined variable: breakfastitem in C:\wamp\www\welcomeforms.php on line 26
100
这些表格与我的眼睛基本相同,所以我不明白为什么一个发布正确的值而另一个没有发布任何内容。我错过了什么荒谬的东西吗?
感谢您的任何建议
答:
0赞
n-dru
3/16/2015
#1
您正在使用两个单独的表单,一次只能提交一个表单。将它们合二为一。
此外,它不仅会使用,还会执行您打算在现代浏览器中获得的功能,请参阅支持:http://www.html5rocks.com/en/tutorials/forms/html5forms/#toc-other-form-attrvalue="Add new breakfast item" onFocus="this.value=''"
placeholder = "Add new breakfast item"
评论
0赞
n-dru
3/16/2015
@JayBlanchard我知道,但我应该在这里使用哪个其他参考?
0赞
Lucy Walsh
3/16/2015
非常感谢,我自己没有注意到这一点,我感到非常愚蠢!已将两种形式合二为一,它可以工作,也感谢您提供有关占位符的提示
0赞
Rajnikant Sharma
3/16/2015
#2
因为它一次只提交一个表单,所以它只从一个表单中获得价值。
评论