我非常新的PHP和一個小的貨幣轉換頁面上工作實踐。我的PHP和HTML代碼都在一個頁面名爲「的index.php」:PHP/HTML後無法正常運行
<?php
$originalAmount = $_POST["originalAmount"];
$convertedAmount = 0.00;
$currency1 = $_POST["currency1"];
$currency2 = $_POST["currency2"];
function convertCurrency($input, $origCurr, $convertCurr) {
if ($origCurr === 1){
if ($convertCurr === 2){
$convertedAmount = $input * 0.78;
return $convertedAmount;
}
elseif ($convertCurr === 3){
$convertedAmount = $input * 0.90;
return $convertedAmount;
}
else {
$convertedAmount = $input;
return $convertedAmount;
}
}
if ($origCurr === 2){
if ($convertCurr === 1){
$convertedAmount = $input * 1.29;
return $convertedAmount;
}
elseif ($convertCurr === 3){
$convertedAmount = $input * 1.16;
return $convertedAmount;
}
else {
$convertedAmount = $input;
return $convertedAmount;
}
}
if ($origCurr === 3){
if ($convertCurr === 2){
$convertedAmount = $input * 0.86;
return $convertedAmount;
}
elseif ($convertCurr === 1){
$convertedAmount = $input * 1.11;
return $convertedAmount;
}
else {
$convertedAmount = $input;
return $convertedAmount;
}
}
}
$convertedAmount = convertCurrency($originalAmount, $currency1, $currency2);
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Currency Converter</title>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<!--Bootstrap 3-->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!--Custom CSS-->
<link href="main.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<!--Header Row-->
<div class="row page-header">
<div class="col-xs-6">
<h1>Currenccy Converter</h1></div>
<div class="col-xs-6">
<p>Add items via the input below.
<br>Click to mark complete (Completed Items will be marked with a green background.)
<br>To delete a task, Click on the X.
<br>Double Click to edit the item, it will be moved to the add item box</p>
</div>
</div>
<!--Add Task/Input Row-->
<div class="row">
<div class="col-xs-12 well">
<h2 class="text-xs-center"><?php print $convertedAmount; ?></h2>
<form class="form-control" id="converterForm" action="index.php" method="post">
<select name="currency1" id="currency1">
<option value="zzz">Starting</option>
<option value="1">USD</option>
<option value="2">GBP</option>
<option value="3">EUR</option>
</select>
<select name="currency2" id="currency2">
<option value="zzz">Converted</option>
<option value="1">USD</option>
<option value="2">GBP</option>
<option value="3">EUR</option>
</select>
<div class="form-group">
<label for="originalAmount" id="origLabel">Amount:</label>
<input id="originalAmount" type="number" value="0.00" name="originalAmount">
<input type="submit" id="submit" name="submit" value="SUBMIT">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
我的問題是,它似乎並沒有在服務器上運行。我曾經有一個只讀輸入,我試圖通過php echo
作爲值更新,但是當我點擊提交時,它不起作用。然後我用包含echo
的H2元素替換輸入。
我對這個頁面的目標是完成通過HTML和PHP的所有行動,而不的JavaScript/AJAX的幫助。
是否有任何超級明顯的理由爲什麼echo
的值不會返回$converted
的值?我寧願使用CSS的readonly輸入和樣式,而不使用H2元素,但是這兩種方法都可以工作。
期待你罰款人認爲。謝謝。
啊。是的,我知道== vs ===,但我錯誤地認爲,因爲我使用輸入類型編號,它將在不轉換的情況下提取int。衛生署。謝謝! –