0

我正在学习一本书的教程,但我被卡住了。

这是 html 订单 (orderform.html)

<!doctype html>
<html>
<head>

</head>

<body>
<form action='processorder.php' method="post">
<table border=0>
<tr bgcolor=#cccccc>
<td width=150>Item</td>
<td width=15>Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align=center><input type="text" name="tireqty" size=3 maxlength=3></td>
</tr>
<tr>
<td>Oil</td>
<td align=center><input type="text" name="oilqty" size=3 maxlength=3></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align=center><input type="text" name="sparkqty" size=3 maxlength=3></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="submit order"></td>
</td>
</table>
</form>

这是流程订单表(processorder.php)

    <!doctype html>
<html>
<head>
<title>Bobs auto parts - Order Results</title>
</head>

<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ";
echo date("1 jS \of F Y h:i:s: A");
echo "<br>";

echo "<p>Your order is as follows:";
echo "<br>";
echo $tireqty." tires<br>";
echo $oilqty." bottles of oil<br>"; 
echo $sparkqty." spark plugs<br>"; 

var_dump($_POST);

?>

当我在 processorder.php 上 var_dump $_POST 时,它说它们在数组中,但是当我尝试回显变量时,它给了我一个通知,说我试图结转的变量是未定义的。请帮忙 :(

4

3 回答 3

4

你必须回应这些:

if (isset($_POST['tireqty']))
{
    echo $_POST['tireqty'];
}
if (isset($_POST['oilqty']))
{
    echo $_POST['oilqty'];
}
if (isset($_POST['sparkqty']))
{
    echo $_POST['sparkqty'];
}
于 2013-07-21T19:47:58.750 回答
0

您需要从发布地图中获取变量。

// set the variable $tireqty to what tireqty was set to in the post call
$tireqty = $_POST['tireqty']; 

更好的是,检查是否通过邮寄方式发送了tireqty:

// set the variable $tireqty to what tireqty was set to in the post call, if it is unset then set an error message
$tireqty = isset($_POST['tireqty'])? $_POST['tireqty'] : "Error message";

一个非常糟糕和危险的方法(请不要尝试这个)是:

// any value passed in via post will get turned into a variable of the same name
extract($_POST);
于 2013-07-21T19:49:22.467 回答
0

你没有定义你的变量,但我已经在代码检查中为你做了。

<!doctype html>
<html>
<head>

</head>

<body>
<form action='processorder.php' method="post">
<table border=0>
<tr bgcolor=#cccccc>
<td width=150>Item</td>
<td width=15>Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align=center><input type="text" name="tireqty" size=3 maxlength=3></td>
</tr>
<tr>
<td>Oil</td>
<td align=center><input type="text" name="oilqty" size=3 maxlength=3></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align=center><input type="text" name="sparkqty" size=3 maxlength=3></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="submit order"></td>
</td>
</table>
</form>

 <!doctype html>
<html>
<head>
<title>Bobs auto parts - Order Results</title>
</head>

<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ";
echo date("1 jS \of F Y h:i:s: A");
echo "<br>";
/*I defined variables bellow */
$tireqty=(isset($_POST['tireqty']))? trim($_POST['tireqty']): '';
$oilqty=(isset($_POST['oilqty']))? trim($_POST['oilqty']): '';
$sparkqty=(isset($_POST['sparkqty']))? trim($_POST['sparkqty']): '';

echo "<p>Your order is as follows:";
echo "<br>";
echo $tireqty." tires<br>";
echo $oilqty." bottles of oil<br>"; 
echo $sparkqty." spark plugs<br>"; 

?>
于 2013-07-21T20:25:39.060 回答