如何显示表单的答案?

时间:2012-03-25 10:38:53

标签: php forms echo calculator

如果我试图在同一页面上发布答案,它会是这样的吗?不太确定我是否使用了正确的功能,请检查我的代码:

<form method="post" action="activity.php">
<input type="text" name= "num1" value="Enter a number"/>
    <select name= "conversion">
        <option>Select a conversion</option>
        <option name="lb to kg">lbs to kgs</option>
        <option name="kgs to lbs">kgs to lbs</option>
        <option name="cm to in">cms to inchs</option>
        <option name="inchs to cms">inchs to cms</option>
        <option name="pints to litres">pints to litres</option>
        <option name="litres to pints">litres to pints</option>
        <option name="faranheit to centigrade">faranheit to centigrade</option>
        <option name="centigrade ti faranheit">centigrade to faranheit</option>
    </select>
    <input type="submit" value="convert" />
    </form>

然后php形式:     

$num1 = $_GET["num1"];
$conversion = $_POST["conversion"];

if($conversion == "lbs to kgs")
{
$answer_lb = $num1 * 0.45;
echo $answer_lb;
}
if($conversion == "kgs to lbs")
{
$answer_kg = $num1 * 2.2;
echo $answer_kg;
}
if($conversion == "cms to inchs")
{
$answer_inch = $num1 * 2.54;
echo $answer_inch;
}

if($conversion == "inchs to cms")
{
$answer_cm = $num1 * 0.393;
echo $answer_cm;
 }
 if($conversion == "pints to litres")
 {
$answer_pints = $num1 * 0.568;
echo $answer_pints;
 }
if($conversion == "litres to pints")
{
$answer_litres = $num1 * 1.579;
echo $answer_litres;
}
 if($conversion == "faranheit to centigrade")
{
$answer_faranheit = ($num1 - 32) * (5/9);
echo $answer_faranheit;
}
if($conversion == "centigrade to faranheit")
{
$answer_centigrade = ($num1 * 9/5) + 32;

 }

?>

<em>

我不确定php代码是否必须在表单之前或之后出现,并且不确定表单操作。

3 个答案:

答案 0 :(得分:1)

//根据您的需求编辑答案:

以下是以下代码的工作示例:http://codepad.viper-7.com/vu6j0N

<?php
$calculation_models = array();
  $calculation_models[] = array('description'=>'lb to kg', 
                                'calculation' => function($val) { return $val * 0.45; }
                               );
  $calculation_models[] = array('description'=>'kgs to lbs', 
                                'calculation' => function($val) { return $val * 2.2; }
                               );
  $calculation_models[] = array('description'=>'cm to in', 
                                'calculation' => function($val) { return $val * 2.54; }
                               );
  $calculation_models[] = array('description'=>'inchs to cms', 
                                'calculation' => function($val) { return $val * 0.393; }
                               );
  $calculation_models[] = array('description'=>'pints to litres', 
                                'calculation' => function($val) { return $val * 0.568; }
                               );
  $calculation_models[] = array('description'=>'litres to pints', 
                                'calculation' => function($val) { return $val * 1.579; }
                               );
  $calculation_models[] = array('description'=>'faranheit to centigrade', 
                                'calculation' => function($val) { return ($val - 32) * (5/9); }
                               );
  $calculation_models[] = array('description'=>'centigrade to faranheit', 
                                'calculation' => function($val) { return ($val * 9/5) + 32; }
                               );

  if (isset($_GET['val']) && isset($_GET['model'])) {
    $result = $calculation_models[$_GET['model']]['calculation']($_GET['val']);
    echo 'The result is ' . $result;
  }
?>

<!-- build the form dynamically from the array above -->

<form method="get" action="#">
  <input type="text" name="val" placeholder="Enter a number"/>
  <select name="model">
    <?php foreach($calculation_models as $key => $model) : ?>
      <option value="<?php echo $key; ?>"><?php echo $model['description']; ?></option>
    <?php endforeach; ?>
  </select>
  <input type="submit" value="convert" />
</form>

答案 1 :(得分:0)

如果你问这个问题,那就不简单了。在不重新加载新页面的情况下更改页面内容通常涉及一种称为AJAX的技术, 基本上,它使用javascript来提交请求,javascript将在收到后再次处理响应。

答案 2 :(得分:0)

我假设您必须使用以下文件:calculator.html和calculator.php

calculator.html就是表格。

使结果显示在同一页面上的第一个解决方案很简单:

将所有内容都放入caclculator.php中,就像这样

    <?php
    if($_GET["submit"]){
    #your code
    }
    ?>
    <html>
    <!-- your html -->
    </html>

如果这不是您要求的,则下一种方法是使用iframe“with the result”和javascript将您的数据发送到iframe。

最后一个解决方案是普通的ajax。