0

I'm writing now very simple currency converter with fixed values of convertions. I'm trying to make it work within a single file containing php script for calculations and a form sending data via post. My code at now looks like this:

<?php
$eurToUsd=1.11;
$usdToEur = 0.94;
$eurToPln = 4.39;
$plnToEur = 0.28;
$usdToPln = 3.87;
$plnToUsd = 0.23;

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

$eurToUsdVal=$_POST['EURtoUSD'];
$usdToEurVal=$_POST['USDtoEUR'];
$eurToPlnVal=$_POST['EURtoPLN'];
$plnToEurVal=$_POST['PLNtoEUR'];
$usdToPLNVal=$_POST['USDtoPLN'];
$plnToUsdVal=$_POST['PLNtoUSD'];
    if ($_POST['EURtoUSD']) {
        echo $eurToUsdVal*$eurToUsd;
    } elseif ($_POST['USDtoEUR']) {
        echo $usdToEurVal*$usdToEur;
    } elseif ($_POST['EURtoPLN']) {
        echo $eurToPlnVal*$eurToPln;
    } elseif ($_POST['PLNtoEUR']) {
        echo $plnToEurVal*$plnToEur;
    } elseif ($_POST['USDtoPLN']) {
        echo $usdToPlnVal*$usdToPln;
    } elseif ($_POST['PLNtousUSD']) {
        echo $plnToUsdVal*$plnToUsd;
    }
}


?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="#" method="POST">
    <label>
        Amount:
        <input type="number" min="0.00" step="0.01" name="cashAmount">
    </label>
    <label>
        Conversion:
        <input type="radio" name="conversionType" value="EURtoUSD" checked> EUR → USD <br>
        <input type="radio" name="conversionType" value="USDtoEUR" > USD → EUR <br>
        <input type="radio" name="conversionType" value="EURtoPLN" > EUR → PLN <br>
        <input type="radio" name="conversionType" value="PLNtoEUR" > PLN → USD <br>
        <input type="radio" name="conversionType" value="USDtoPLN" > USD → PLN <br>
        <input type="radio" name="conversionType" value="PLNtoUSD" > PLN → USD <br>
    </label>
    <input type="submit">
</form>

</body>
</html>

when i'm trying to use this converter i am getting error telling "Undefined index: EURtoUSD" and the same for other conversions. To be precise i'm getting list of errors like this for all convertions in this php file. What do i miss here?

spectatorx
  • 373
  • 2
  • 7
  • 22

1 Answers1

3

it's pretty sure you will have an error "Undefined index: EURtoUSD", because the name of your input text is conversionType.

so what you have to do is remove this

$eurToUsdVal=$_POST['EURtoUSD'];
$usdToEurVal=$_POST['USDtoEUR'];
$eurToPlnVal=$_POST['EURtoPLN'];
$plnToEurVal=$_POST['PLNtoEUR'];
$usdToPLNVal=$_POST['USDtoPLN'];
$plnToUsdVal=$_POST['PLNtoUSD'];

and change the condition like this

if ($_POST['conversionType'] == "EURtoUSD") {
    echo $eurToUsd*$_POST['conversionType'];
// and so on
zaidysf
  • 492
  • 2
  • 14
  • Thx, i edited my code following your suggestion but now every time as output i get 0, no matter what i input. – spectatorx Aug 18 '16 at 13:04
  • try to `var_dump($_POST['conversionType'])` and let us know the result – zaidysf Aug 18 '16 at 13:20
  • For test i did input 22 and vardump outputted this: string 'EURtoUSD' (length=8) – spectatorx Aug 18 '16 at 13:27
  • Anyone have any ideas how to solve this issue and properly send input via post method in this case? Seems like value is being send and received instead of input. – spectatorx Aug 18 '16 at 19:52
  • 1
    it should be `$_POST['cashAmount'] * $eurToUsd` – zaidysf Aug 19 '16 at 06:09
  • Yes Zaid, you are right. That's where i had mistake in my code, i was picking wrong variable to my calculation and i was picking meaningless string instead of proper value. – spectatorx Aug 19 '16 at 16:19