Hello,I am starting PHP.So please help me.This code for a simple convertor in php just convert Dollar to local currency.here my Elseif not correctly working.here is code:

<html>
<head>
<title>currency Convert</title>
</head>
<body>
<form action="" method="POST">
      <table>
         <tr>
         <td>
             <lebel>$</label>
             <input type="text" name="amount"/>
             to <select name="form">
                          <option value="taka">taka</option>
                          <option value="rupe">rupe</option>
                          <option value="inr">inr</option>

             </select>
             <input type="submit" value="convert"/>
         </td>
         </tr></form>
         <tr>
         <td>
         <?php
         $amount=$_POST['amount'];
         if($amount>0){
            $currency = $amount*80;
            echo "you converted Amount $".$amount. " = ".$currency."Tk";

         }
         elseif($amount>0){
            $currency = $amount*20;
            echo "you converted ammount $".$convert. " = ".$currency."Rup";
         }
         elseif($amount>0){
            $currency = $amount*10;
            echo "you converted ammount $".$convert. " = ".$currency."Inr";
         }
         else
         echo 'enter your ammount';
         ?>
         </td>
         </tr>


      </table>
</body>
</html>

why you are having same conditions in your else if's ??
It can be done by nested ifs. do another check in your ifs i.e. from which currency to what currency??

If else statements are executed base on the condition specified. Looking at your code, whenever the amount is greater than 0, it will always match the first if block. if the amount isn't greater than 0, it will always match the else block.

You might consider trying a switch statement instead and use the value of $_POST['form'] where you select your currency to determine the output. E.g.

$amount = isset($_POST['amount']) ? $_POST['amount'] : 0;
$currency = isset($_POST['form']) ? $_POST['form'] : false;
$value = 0;

if(! $amount) {
    echo 'Please enter an amount';
}
else {
    switch($currency) {
        case 'taka':
            $value = ($amount * 80) . 'Tk';
        break;
        case 'rupe':
            $value = ($amount * 20) . 'Rup';
        break;
        case 'inr':
            $value = ($amount * 10) . 'Inr';
        break;
        default:
            echo 'Please select a currency';
        break;
    }
}

yes,I will use switch statement..Thanks all

Member Avatar for diafol

You also have a typo here:

<lebel>$</label>

'label'. Sorry, bit off-topic.

<html>
<head>
<title>currency Convert</title>
</head>
<body>
<?php
$amount = (isset($_POST['amount'])) ? $_POST['amount'] : 0;
$slt1 = (isset($_POST['form']) && $_POST['form']=="taka") ?  'selected="selected"'  : '';
$slt2 = (isset($_POST['form']) && $_POST['form']=="rupe") ?  'selected="selected"'  : '';
$slt3 = (isset($_POST['form']) && $_POST['form']=="inr") ?  'selected="selected"'  : '';
?>
<form action="" method="POST">
      <table>
         <tr>
         <td>
             <lebel>$</label>
             <input type="text" name="amount"  value="<?php echo $amount; ?>"  />
             to <select name="form">
                          <option <?php echo $slt1;?> value="taka">taka</option>
                          <option <?php echo $slt2;?> value="rupe">rupe</option>
                          <option <?php echo $slt3;?> value="inr">inr</option>

             </select>
             <input type="submit" value="convert"/>
         </td>
         </tr></form>
         <tr>
         <td>
<?php
 if(isset($_POST['amount'])) {
 $amount=$_POST['amount'];
 $currencyCode=$_POST['form'];
 if($amount>0){
    if($currencyCode=='taka') {
    $currency = $amount*80;
    echo "you converted Amount $".$amount. " = ".$currency."Tk";

     }
    elseif($currencyCode=='rupe'){
    $currency = $amount*20;
    echo "you converted ammount $".$amount. " = ".$currency."Rup";
    }
    elseif($currencyCode=='inr'){
    $currency = $amount*10;
    echo "you converted ammount $".$amount. " = ".$currency."Inr";
    }

 }
 else
 echo 'enter your ammount';
 }
         ?>
         </td>
         </tr>


      </table>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.