dongtuo6562 2016-10-21 16:58
浏览 7

混合表单和函数(php)

I'm working on my first project and self-teaching myself through the entire things, so please excuse this uber basic question.

How do I make the submit work within the page?

Currently, I have: the function:

$tempC = 32;
$tempF = $tempC * 1.8 + 32;
echo $tempF;

And then I am creating the form:

<form>
    <input type="text" name="Celsius">
    <input type="submit" value="Calculate"
</form>

How do I link the php variables to the form?

ETA: My hope is to be able to enter Celsius into the field and have the Fahrenheit populate below it. I've been following some tutorials but in an effort to avoid copying and pasting w/o understanding, I've just decided to handwrite it from scratch myself...Trial and error, you know. In any case, here's where I'm at now...the button isn't updating the values. :-/

function runConversion () {
$tempC = 32;
$tempF = 0;  
$tempF = $tempC * 1.8 + 32;
echo $tempF . "F ";
echo $tempC . " C";
}
?>

<?php runConversion(); ?>
<form action="DT-Converter.php" method="post">
    <input type="text" name="Celsius">
    <input type="submit" name="convert" value="Calculate">
</form>
<?php
    if(isset($_POST['convert'])) {
        $tempC = $_POST['Celsius'];
        runConversion();
    }
  • 写回答

2条回答 默认 最新

  • doucuodan0897 2016-10-21 17:01
    关注

    Assuming you are trying to make the temperature value appear in the form just use this

    <form>
        <input type="text" name="Celsius" value="<?=$tempF?>">
        <input type="submit" value="Calculate"
    </form>
    

    The <?= operator makes php easier to use as a templating language as it just echos the variable into the form.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大