如何显示表单的答案


How to display answer to a form?

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

<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代码是否必须出现在表单之前或之后,也不确定表单的操作。

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

下面是一个代码的工作示例: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>

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

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

calculator.html是形式.

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

像这个一样把所有东西都放进cacalculator.php

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

如果这不是您所要求的,那么下一个方法是使用iframe"with the result"和javascript将数据发送到iframe。

最后一个解决方案是简单的ajax。