PHP使用POST未定义的变量



我得到了这个。。。

Notice: Undefined index: cost, sku, costMethod

我认为是验证部分打破了这一点。我已经测试了在没有验证的情况下发送这些变量,并且它们收到得很好。我相信这是当一切都是有效的,并且标头给它的目的地是它丢失变量的地方。

这是我的表单代码:

<!DOCTYPE HTML>
<html>
<head>
<title>Shipping Overrides Client</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#additive").click(function(){
    $("#cost").attr("value","0.00");
  });
});
</script>
</head>
<body>
<?php
// define variables and set to empty values
$message = "SKU is required!";
$skuErr = "";
$sku = "";
$costErr ="";
$cost = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$valid = true;
if (empty($_POST["sku"]))
     {$skuErr = "SKU is required";
     $valid = false;}
   else
   {$sku = $_POST["sku"];}
   if (empty($_POST["cost"]))
     {$costErr = "Cost is required";
     $valid = false;
     }
   else
  {$cost = $_POST["cost"];}
  if(isset($_POST['costMethod'])){
  $costMethod = $_POST["costMethod"];
  }
 if($valid){
header('Location: SubmitFeedSample.php?sku=$sku&cost=$cost&costMethod=$costMethod');
exit();
}
}
?>
<h1>Shipping Override Client</h1>
<form method="post" action="index.php" >
SKU: <input type="text" name="sku" value="<?php echo $sku ?>">* <?php echo $skuErr;?><br>
STD Shipping Cost: $ <input type="text" name="cost" id="cost" value="<?php echo $cost ?>">* <?php echo $costErr;?><br>
<input type="radio" name="costMethod" id="exclusive" value="Exclusive" checked>Override 
<input type="radio" name="costMethod" id="additive" value="Additive" >Delete Existing Override <br>
<input type="submit" name= "submit" value="Submit">
</form>
</body>
</html>

这里是SubmitFeedSample.php:

<?php
 $shippingCost = $_POST["cost"];
 $sku = $_POST["sku"];
 $costMeth = $_POST["costMethod"];
echo $shippingCost . "<br>";
echo $sku . "<br>";
echo $costMeth . "<br>";
var_dump($_POST);
 ?>

有效时如何获取要发送的变量?

您在此处提出GET请求-

header('Location: SubmitFeedSample.php?sku=$sku&cost=$cost&costMethod=$costMethod');

尝试更改此-

<?php
 $shippingCost = $_GET["cost"];
 $sku = $_GET["sku"];
 $costMeth = $_GET["costMethod"];
 echo $shippingCost . "<br>";
 echo $sku . "<br>";
 echo $costMeth . "<br>";
 var_dump($_POST);
 ?>

一旦标头已经发送,就不能使用header重定向:

header('Location: SubmitFeedSample.php?sku=$sku&cost=$cost&costMethod=$costMethod');

这就是为什么你可能没有得到结果。此外,这不是获取post数据的正确方法。如果您像这样在URL中传递参数,那么您使用的是GET而不是POST。

最新更新