-3

I am trying to create a small form which adds certain amounts from an html form The code is found below

HTML Page-->

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content ="width=device-width,initial-scale=1,user scalable=yes" />
<title>NMAWS Form</title>
<style type="text/css">
div#container
{
   width: 800px;
   position: relative;
   margin-top: 0px;
   margin-left: auto;
   margin-right: auto;
   text-align: left;
}
</style>
<style type="text/css">
body
{
   text-align: center;
   margin: 0;
   background-color: #FFFFFF;
   color: #000000;
}
</style>

</head>
<body>
<form method="post" attribute="post" action="calcsubmit.php">
<br>
<b>Name:<br>
<input type="text" id="name" name="name"><br><br>
Email:<br>
<input type="text" id="email" name="email"><br><br>
Cell Number:<br>
<input type="text" id="cell" name="cell"><br>

<p>Number of people in home:<br/>
<select name="occupants" id="occupants" style="width:100px"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option>  <option value="5">5</option>  <option value="6">6</option>  <option value="7">7</option>  <option value="8">8</option>  <option value="9">9</option><option value="10">10</option></select></p>

<p>How much do you spend on<br>bottled water each week?<br/>
<select name="water" id="water" style="width:100px"><option value="5">$0 to $5</option><option value="10">$5 to $10</option><option value="15">$10 to $15</option><option value="20">$15 to $20</option><option value="25">$20 to $25</option></select></p>

<p>How many loads of laundry<br>do you do each week?<br/>
<select name="laundry" id="laundry" style="width:100px"><option value="1">1</option><option value="2">2</option><option value="3">3</option>    <option value="4">4</option>  <option value="5">5</option>  <option value="6">6</option>  <option value="7">7</option>  <option value="8">8</option>  <option value="9">9</option><option value="10">10</option></select></p>

<p></p>
<button type="submit" name="answer" id="answer" value="answer" style="width:100px; height:100px;"><font size='4'>Calculate Savings</font></button>
</form>
</body>
</html>

PHP page--->

<html>
<head>
<meta charset="utf-8">
<title>Savings</title>
</head>
<body>
<p>Savings are: 
<?php
if($_POST["answer"])
{
echo "$water + $laundry + $occupants";
}
?>
</p> 
</body>
</html>

The result is always + +, the php isnt even attempting to do the equation I am really not seeing what is wrong here

Koden
  • 353
  • 1
  • 13

3 Answers3

1

You haven't declared your variables anywhere. You're probably looking for the $_POSTed variables.

If you want to return the equation:

<?php
if($_POST["answer"])
{
echo $_POST['water'] . ' + ' . $_POST['laundry'] . ' + ' . $_POST['occupants'];
}
?>

or if you want to return the answer

<?php
if($_POST["answer"])
{
echo $_POST['water'] + $_POST['laundry'] + $_POST['occupants'];
}
?>
cameronjonesweb
  • 2,435
  • 3
  • 25
  • 37
0

Specifically on your question. To get the sum of values. You need to lose the quotes

echo $water + $laundry + $occupants;

Now, overall. There are a few points i'd like to mention. You are using $_POST, so on your secondary page. You need to reference the array by:

echo $_POST['water'] + $_POST['laundry'] + $_POST['occupants']; 

Second:

I would not use if ($_POST['answer']). Instead i'd:

if (isset($_POST)){ 

echo $_POST['water'] + $_POST['laundry'] + $_POST['occupants'];

}
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
0

Loose the quotes "" and $water, $laundy, $occupant are not defined, you should access those values through $_POST instead

<?php
if($_POST["answer"]){ 
  //this will sum the three values.
  echo $_POST["water"] + $_POST["laundry"] + $_POST["occupants"];;
}
?>
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98