几乎相同的HTML表单,一个发布正确的值,另一个则不然


Near identical HTML forms, one posts correct value, the other doesn't

我的页面上的两个表单欢迎.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>

这是欢迎表单上的代码.php:

if(isset($_POST['breakfast'])){$breakfastitem = $_POST['breakfast'];}
if(isset($_POST['breakfast_calories'])){$breakfastcals = $_POST['breakfast_calories'];}
echo $breakfastitem;
echo $breakfastcals;
表单在网页上

正确显示,当我在早餐表单中输入值"面包"并在早餐表单中输入值"100"时,这是网页上返回的内容:

Undefined variable: breakfastitem in C:'wamp'www'welcomeforms.php on line 26
100

这些表格与我的眼睛基本相同,所以我不明白为什么一个发布正确的值而另一个不发布任何内容。我错过了什么荒谬的东西吗?

感谢您的任何建议

您使用的是两个单独的表单,一次只能提交一个表单。将它们合二为一。

此外,value="Add new breakfast item" onFocus="this.value=''"只使用placeholder = "Add new breakfast item" - 它将执行您打算在现代浏览器中获得的内容 - 请参阅支持:http://www.html5rocks.com/en/tutorials/forms/html5forms/#toc-other-form-attr

因为它一次只提交一个表单,所以它只从一个表单中获取值。

相关文章: