Home > OS >  if else statement not posting variable
if else statement not posting variable

Time:09-27

in this code i am trying to make a simple form asking user which kind of envelope they want. after the user picks an option in the form i want it to display a total. the $shipment variable should be what holds the price and the issue is it is not printing to screen when i echo the total. Everything else in this code is showing on screen as intended besides $shipment. any tips on what is wrong is appreciated!

<html>
<body>

<form action="" method="post">
Package Type :  
<select name="package" id="package" value="type">  
  <option value="Flate Rate Envelope">Flate Rate Envelope</option>}  
  <option value="Small Rate Envelope">Small Rate Envelope</option>  
  <option value="Medium Rate Envelope">Medium Rate Envelope</option>  
  <option value="Large Rate Envelope">Large Rate Envelope </option>  
</select>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

<?php

echo "<br>"; 
echo $_POST["package"];
echo "<br>";
$shipment = 0;
if ($_POST["package"] == 'Flate Rate Envelope'){
    $shipment = 6.70;
    echo "Total Shipping Cost: $".$_POST[$shipment];
}
else if ($_POST["package"] == 'Small Rate Envelope'){
    $shipment = 7.20;
    echo "Total Shipping Cost: $".$_POST["$shipment"];
}
else if ($_POST["package"] == 'Medium Rate Envelope'){
    $shipment = 13.65;
    echo "Total Shipping Cost: $".$_POST["$shipment"];
}
else if ($_POST["package"] == 'Large Rate Envelope'){
    $shipment = 18.90;
    echo "Total Shipping Cost: $".$_POST["$shipment"];
}

?>

CodePudding user response:

You are echoing the variable $_POST[$shipment], which does not exist. Change it to $shipment.

echo "<br>"; 
echo $_POST["package"];
echo "<br>";
$shipment = 0;
if ($_POST["package"] == 'Flate Rate Envelope'){
    $shipment = 6.70;
    echo "Total Shipping Cost: $".$shipment;
}
else if ($_POST["package"] == 'Small Rate Envelope'){
    $shipment = 7.20;
    echo "Total Shipping Cost: $".$shipment;
}
else if ($_POST["package"] == 'Medium Rate Envelope'){
    $shipment = 13.65;
    echo "Total Shipping Cost: $".$shipment;
}
else if ($_POST["package"] == 'Large Rate Envelope'){
    $shipment = 18.90;
    echo "Total Shipping Cost: $".$shipment;
}
?>

P.S. Don't do it in this way. It is both unsafe as well as a buggy piece of code. Also, check your $POST variabe against SQL injection.

  •  Tags:  
  • php
  • Related