如何得到回声返回方程式?

时间:2015-09-29 23:45:33

标签: php forms

我正在尝试创建一个从html表单中添加一定数量的小表单 代码见下面

HTML页面 - >

<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页面---&gt;

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

结果总是+ +,php甚至不试图做等式 我真的没有看到这里有什么问题

3 个答案:

答案 0 :(得分:1)

您尚未在任何地方声明变量。您可能正在寻找$_POST ed变量。

如果你想返回等式:

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

或者如果你想回答

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

答案 1 :(得分:0)

特别是关于你的问题。获得值的总和。你需要丢失报价

echo $water + $laundry + $occupants;

现在,整体而言。我想提一点几点。您正在使用$_POST,因此在您的辅助页面上。您需要通过以下方式引用该数组:

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

第二:

我不会使用if ($_POST['answer'])。相反,我是:

if (isset($_POST)){ 

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

}

答案 2 :(得分:0)

松散引号""$water$laundy$occupant未定义,您应该通过$_POST来访问这些值

<?php
if($_POST["answer"]){ 
  //this will sum the three values.
  echo $_POST["water"] + $_POST["laundry"] + $_POST["occupants"];;
}
?>