PHP未定义的变量与POST

时间:2014-04-18 14:51:37

标签: php

我得到了这个......

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);
 ?>

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

2 个答案:

答案 0 :(得分:1)

您正在此处发出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);
 ?>

答案 1 :(得分:0)

一旦发送了标题,您就无法使用header进行重定向:

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

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