亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 和 HTML 表單 - 輸入字段中的隨機值

PHP 和 HTML 表單 - 輸入字段中的隨機值

PHP
鳳凰求蠱 2021-06-17 09:19:27
我寫了一個從攝氏度到華氏度的轉換,反之亦然。問題是現在在表單字段中向我顯示隨機值。如何轉換此代碼,以便在輸入值后,第二個字段中的轉換值出現在第一個字段中?是否可以只使用 php?if(isset($_POST['fah'])){    $cel=($_POST['fah']+32)/1.8;}if(isset($_POST['cel'])){    $fah=($_POST['cel']-32)*1.8;}?><html><body><form method="post" action="calc.php">Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel"><input type="submit" value="Calc"></form>我希望在第一個字段中輸入的值顯示在第二個轉換中。
查看完整描述

3 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

你可以試試這個例子:


$celValue = $_POST['cel'];

$fahValue = $_POST['fah'];


if( ! empty($fahValue)){

    $celValue = fahrenheitToCelcius($_POST['fah']);

}

if( ! empty($celValue)){

    $fahValue = celciusToFahrenheit($_POST['cel']);

}


function celciusToFahrenheit($cel) {

    return ($cel - 32) * 1.8;

}


function fahrenheitToCelcius($fah) {

    return ($fah + 32) / 1.8;

}


?>

<html>

<body>



<form method="post" action="calc.php">

Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $celValue; ?>" name="fah">

Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $fahValue; ?>" name="cel">

<input type="submit" value="Calc">


</form>


查看完整回答
反對 回復 2021-06-19
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

嘗試這樣的事情


$cel="";

$fah="";


if(isset($_POST['fah']) && !empty($_POST['fah'])){

    $cel=($_POST['fah']+32)/1.8;

}

if(isset($_POST['cel']) && !empty($_POST['cel'])){

    $fah=($_POST['cel']-32)*1.8;

}


查看完整回答
反對 回復 2021-06-19
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

如果你發回自己,你可以做所有的 php。如果輸入的頁面是 calc.php 加一個 else 把值設置為空字符串。


if(isset($_POST['fah'])){

    $cel=($_POST['fah']+32)/1.8;

} else {

    $cel = '';

}

if(isset($_POST['cel'])){

    $fah=($_POST['cel']-32)*1.8;

} else {

    $fah = '';

}


查看完整回答
反對 回復 2021-06-19
  • 3 回答
  • 0 關注
  • 196 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號