2016-07-07 106 views
0

我非常新的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元素,但是這兩種方法都可以工作。

期待你罰款人認爲。謝謝。

回答

2

問題:如果條件中使用了===,則使用===來比較變量的值和類型。在你的情況$_POST['your_variable']是字符串類型,你正在比較字符串與數字。 if ($origCurr === 1)。閱讀比較運算符here

enter image description here

解決方案:無論是類型轉換變量或使用==只比較值。 if ($origCurr == 1)

+0

啊。是的,我知道== vs ===,但我錯誤地認爲,因爲我使用輸入類型編號,它將在不轉換的情況下提取int。衛生署。謝謝! –

0

羅希特是當場上爲什麼會出現此錯誤的原因。我還想爲轉換函數提供這種替代方法,但仍需要考慮輸入變量的類型。

function convertCurrency($input, $origCurr, $convertCurr) { 

    $conversionTable = array(
      '1' => array('2' => 0.78, '3' => 0.9), 
      '2' => array('1' => 1.29, '3' => 1.16), 
      '3' => array('2' => 0.86, '1' => 1.11) 
     ); 

     return $input * (($origCurr !== $convertCurr) ? $conversionTable[$origCurr][$convertCurr] : 1); 
} 
+0

好的選擇,我想你錯過了第三例? 'USD <=> USD' – Rohit

+0

@Rohit true!我更新了函數以反映'$ origCurr == $ converCurr'的情況。 – mulquin

+1

爲什麼它應該返回1?它應該是輸入值'return($ origCurr!== $ convertCurr)? $ input * $ conversionTable [$ origCurr] [$ convertCurr]:$ input;' – Rohit