注意:未定义的变量:$ product *输入类型图像作为按钮

时间:2015-11-04 04:35:55

标签: php forms post undefined input-type-file

嗯...我从$ _GET / $ _ POST感到困惑,但我经常使用$ _POST n有时$ _GET用于一对一的html ..这是我的代码:

home.php:

<form action="buy.php" method="POST"> 
<input type="image" name="product" value="Noodle" src="images/Noodle.jpg" height="290" width="333">
    <input type="image" name="product" value="Rice" src="images/Rice.jpg" height="290" width="333">
    <input type="image" name="product" value="tofu" src="images/tofu.jpg" height="290" width="333">
</form>

buy.php:

<?php
$product = $_POST["product"];
echo "You have ordered a bowl of ". $product . '!' . " Do you need to get extra beverages?"; ?>
<div>
<form action="main_menu.php" method="GET">
<input type="submit" name="SUBMIT" value="NO">

  

<h3> Choose extra Menu </h3>
<form action="extra_menu.php" method="GET"> `
<table bgcolor="#B0D8E2" border="0" cellpadding="2" width="180">
<tr> <td valign="middle">
<input type="image" name="name" value="dumplings" src="images/dumplings.jpg" height="300" width="300">
</td>
</tr> 
</table> 
<table bgcolor="#B0D8E2" border="0" cellpadding="2" width="180">
<tr> 
<td valign="middle"> 
<input type="image" name="name" value="cayenne" src="images/cayenne.jpg " height="300" width="300">
</td></tr> 
</table>  

main_menu.php:

  <?php
echo $product . "your main product will be sent soon!";
?>

extra_menu.php:

<?php
echo "you have ordered". htmlspecialchars($_GET["name"]) . '!' . " Please wait a moment!";
?>

仅在main_menu.php上我收到通知:未定义的变量:product 我曾尝试添加$product=$_POST["product"];但仍未定义变量x_x 为什么我在cant认可之前定义的变量? 0w0

2 个答案:

答案 0 :(得分:0)

HOME.PHP

<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data"/>
<input type="text" name="product[]" value="Noodle"/> 
<input type="image" name="productImg[]"  src="images/Noodle.jpg" height="290" width="333">
<input type="text" name="product[]" value="Rice"/>
<input type="image" name="productImg[]"  src="images/Rice.jpg" height="290" width="333"> 
<button type="submit" name="submit" value="submit">Submit</button>
</form> 
</body>
</html>

buy.php

<?php

$products = $_POST["product"];
foreach($products as $product) {
  echo "You have ordered a bowl of ". $product . '!' . " Do you need to get extra beverages?<BR/>";   
}
?>
<div>
<form action="main_menu.php" method="GET">
<input type="submit" name="SUBMIT" value="NO">
</form>
</div>

答案 1 :(得分:0)

非常简单。

在设置之前,您需要检查$ _POST超全局中是否存在该数组键,否则将其设置为其他值。

$product = isset( $_POST['product'] ) ? $_POST['product'] : false;

使用上述三元组,当您尚未发布product variable时,它会为其分配值false

之后您可以通过检查值来回显$product之前进行检查。

if (FALSE !== $product){
    echo $product;
}