-1

I am getting this...

Notice: Undefined index: cost, sku, costMethod

I think it is the validation part that is breaking this. I have tested sending these variables without the validation and they are received fine. I believe it is when everything is valid and the header gives it the destination is where it is losing the variables.

Here is my Form code:

<!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>

Here is SubmitFeedSample.php:

<?php

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

How can I get the variables to send when valid?

Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78

2 Answers2

1

You are making a GET request here -

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

Try changing this -

<?php
 $shippingCost = $_GET["cost"];
 $sku = $_GET["sku"];
 $costMeth = $_GET["costMethod"];
 echo $shippingCost . "<br>";
 echo $sku . "<br>";
 echo $costMeth . "<br>";
 var_dump($_POST);
 ?>
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
  • You could also consider just making `SubmitFeedSample.php` a part of `index.php`. – Tyler Carter Apr 18 '14 at 14:57
  • @ Tyler - Yes true. Still answering where he/she got stuck or getting error – Parag Tyagi Apr 18 '14 at 14:58
  • More of a comment to the asker than you :) – Tyler Carter Apr 18 '14 at 14:58
  • @ Tyler - True again :D :D – Parag Tyagi Apr 18 '14 at 14:59
  • Okay, changing the SubmitFeedSample.php page to _GET did the trick. I am getting the variables now. However, lets say I just want to use _POST and change the header to header('Location: SubmitFeedSample.php); without the variables in the url. That doesn't seem to work either. How could I get _POST to work? – user3549135 Apr 18 '14 at 15:12
  • You can't do that. As @Tyler suggested above, include your `SubmitFeedSample.php` in your `index.php` – Parag Tyagi Apr 18 '14 at 15:15
  • For making _POST work, you must have to `post` using a form. What you are trying without variables will simply redirects you to that page, thats it. – Parag Tyagi Apr 18 '14 at 15:16
  • @Parag Okay, forget the 'header' routine. I have successfully sent the variables with _POST to the other SubmitFeedSample.php page so there is a way to send variables via POST. There has to be a way to toggle what page the variables are sent to. Either index.php if invalid or SubmitFeedSample.php if valid. – user3549135 Apr 18 '14 at 15:30
  • Still if you want to do, `post` it with AJAX request `$.post()` – Parag Tyagi Apr 18 '14 at 15:33
  • @ParagTyagi I ran into a snag. Using my validation gives me literals of the variables. Not the values. If I test using GET or POST without the validation/header i get the values fine. There must be something wrong with the header converting the variables to strings. – user3549135 Apr 18 '14 at 16:22
  • Got it, nevermind. header('Location: SubmitFeedSample.php?sku='. $sku.'&cost='.$cost.'&cost'.'Method='.$costMethod); – user3549135 Apr 18 '14 at 16:45
0

You cannot redirect with header once headers have already been sent:

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

That's why you are probably not getting the result there. Also that's not a proper way to fetch the post data. If you are passing arguments in URL like that you are using GET not POST.

mareckmareck
  • 1,560
  • 13
  • 18