0

I'm working on a little work on conversion but in reality I'm learning the switch case statement in PHP =D

When I select my country, I can applicate the good rate.

Here is the HTML

<select class="form-control" name="devise">
        <option name="pays" value="USA">USA</option>
        <option name="pays" value="EUR">EUR</option>
        <option name="pays" value="JPY">JPY</option>
</select>

Here is the PHP

if (isset($_POST['conversion'])){
if(!is_numeric($_POST['montant'])){
    echo('<div class="alert alert-danger  style="display:none;">veuillez insérer un nombre</div>');
    exit();
} else if(isset($_POST['pays'])){
    $devise = $_POST['devise'];
    $montant = $_POST['montant'];
    switch($pays){
        case 'USA':
            $resultConvert = $montant *  1.3;
            echo('<div class="alert alert-success"  style="display:none; role="alert">dollar</div>');
            break;
        case 'EUR':
            $resultConvert = $montant * 1.2;
            echo('<div class="alert alert-success" style="display:none; role="alert">euro</div>');
            break;
        case 'JPY':
            $resultConvert = $montant * 1.5;
            echo('<div class="alert alert-success"  style="display:none; role="alert">yen</div>');    
            break;
        default:
            echo('<div class="alert alert-danger style="display:none;">Veuillez choisir une devise</div>');
    }
    return $resultConvert;
} 

} I wanted to return $resultConvert, but I don't have errors from the server. I tried to var_dump() it, but It displays nothing.

Thank you for your help. I want to learn where I did a mistake.

P.S. The rates of convertion are wrong, I just want to display the good result with the good device.

  • please do the minimum of debugging before to ask a question. that means looking at every variable to see if the contains the expected values and writing these values in the question. look also this page : http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – mmm Oct 09 '16 at 13:31
  • You will need to provide more code. You are using $pays in switch but we can't tell what is $pays, it is not defined in this part of code. – Boris Savic Oct 09 '16 at 13:32
  • here `switch($pays){` you'd need `switch($_POST['pays']){`! – Jeff Oct 09 '16 at 13:42
  • and you can only `return` smth if you are inside a function (or an included php). So you might want to echo `$resultConvert` here. – Jeff Oct 09 '16 at 13:43
  • I'm working on the code, I will do the minimum of debugging then edit the question. I will show the entire form in 10 minutes XD But thank you for your propositions –  Oct 09 '16 at 13:52

1 Answers1

0

Replace

switch($pays){ 

with

switch($_POST['pays']){
RahulN
  • 218
  • 1
  • 5
  • I found the solution in another way. I declared $pays = $_POST['pays']; before the switch, so switch($pays). Then I modified the name of the select to "pays" and deletete the "name" attribute from options. –  Oct 09 '16 at 14:09
  • 1
    Both ways it will work, BTW, do not take $_POST variable as it is, always Validate it, sanitise it, and escape html. – RahulN Oct 09 '16 at 14:12