Compare value of a select with a value of an input using if-else

1

I am a newbie programming in PHP and I need to make a form using if-else where the user enters the value of a product and choose a payment plan of a select to calculate the interest and total amount.

The plans are as follows:

  • panel A iteres 10%
  • panel B interest 20%
  • panel C interest 34%
  • panel D interest-free interest

This is the code of the form:

<form action="enviar.php" method="GET">

<select name="opciones" id="">
  <option value="plan1">Plan A</option>
  <option value="plan2">Plan B</option>
  <option value="plan3">Plan C</option>
  <option value="plan4">Plan D</option>
</select>

<br>

Ingrese el monto:
<input type="number" name="dinero"><br>
<input type="submit" value="Enviar">

</form>

And the PHP code that processes it:

<?php

$costo = $_POST["dinero"]."<br>";
$opciones = $_POST["opciones"];

if ($opciones == "plan1") {

  $resultado1 = $costo *10/100;
  $total = $resultado1 + $costo;

  echo "El interes es: ".$resultado3"El valor total es: ".$total;;
}
else ($opciones == "plan2") {

  $resultado2 = $costo *20/100;
  $total = $resultado2 + $costo;

  echo "El interes es: ".$resultado2"El valor total es: ".$total;;

}
else ($opciones == "plan3") {

  $resultado3 = $costo *34/100;
  $total = $resultado3 + $costo;

  echo "El interes es: ".$resultado3. "El valor total es: ".$total;

}
else ($opciones == "plan4") {

  echo "Sin Interes";
?>
    
asked by Sora Kasugano 15.04.2018 в 03:05
source

3 answers

0

I would use a SWITCH, it's more elegant and looks better:

<?php

$costo = $_POST["dinero"]."<br>";
$opciones = $_POST["opciones"];

switch ($opciones)
{
  case "plan1":
    $resultado1 = $costo *10/100;
    $total = $resultado1 + $costo;
    echo "El interes es: ".$resultado3"El valor total es: ".$total;
    break;

  case "plan2":
    $resultado2 = $costo *20/100;
    $total = $resultado2 + $costo;
    echo "El interes es: ".$resultado2"El valor total es: ".$total;
    break;

  case "plan3":
    $resultado3 = $costo *34/100;
    $total = $resultado3 + $costo;
    echo "El interes es: ".$resultado3. "El valor total es: ".$total;
    break;

  //El default se realiza cuando no se ha introducido ninguno de los 
  //anteriores, como puede ser el plan4
  default:
    echo "Sin Interés";
    break;
}
?>

Greetings.

    
answered by 15.04.2018 в 03:32
0

It would be ideal but it asks me to specifically use if-else to then take out the sales commissions that would be:

For amounts under $ 500, 5% of the amount For amounts equal to or greater than $ 500, but less than $ 1000, 7% of the amount. For amounts greater than or equal to $ 10,000, 8% plus $ 6 Pesos

and if I'm not wrong then it would be something like this:

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Ejercicio 3</title>
</head>
<body>

<form action="enviar.php" method="GET">

Ingrese el monto:

<input type="number" name="comision"><br>
<input type="submit" value="Enviar">
</form>

</body>
</html>

//PHP//

<?php

$comision = $_POST["comision"]."<br>";


if ($comision < 500) {

$resultado1 = $comision *5/100;

echo "La comision es: ".$resultado3;
}
else if($comision >= 500 && <1000) {

$resultado2 = $comision *7/100;

echo "La comision es: ".$resultado2;

}

else if($comision >= 10000) {

$resultado3 = $comision *8/100 + 6;

echo "La comision es: ".$resultado3;

}
    
answered by 15.04.2018 в 03:58
0

DETAILED SYNTAX ERRORS I RECOMMEND YOU TO READ THE CLARIFICATIONS I WILL MAKE YOU

  

You are applying it badly, if you have more than one option to do   comparisons you should not use else; if not else if to indicate that   you can compare with more possible options, when you no longer have   scenarios to compare and you will give a default answer then if   apply else, in addition to the else until the end was missing a key to close

<?php

$costo = $_POST["dinero"]."<br>";
$opciones = $_POST["opciones"];

if ($opciones == "plan1") {

$resultado1 = $costo *10/100;
$total = $resultado1 + $costo;

echo "El interes es: ".$resultado3."El valor total es: ".$total;
}
else if($opciones == "plan2") {

$resultado2 = $costo *20/100;
$total = $resultado2 + $costo;

echo "El interes es: ".$resultado2."El valor total es: ".$total;

}

else if($opciones == "plan3") {

$resultado3 = $costo *34/100;
$total = $resultado3 + $costo;

echo "El interes es: ".$resultado3. "El valor total es: ".$total;

}


else if($opciones == "plan4") {

echo "Sin Interes";
}else{
echo "Opción desconocida";  
}

Another error that I notice is that when concatenating a variable between text strings you are not doing it from both sides, that is to say when concatenating like this you must put a point just before and after the variable; in this way .$name. and finally you had double semicolons in some lines

    
answered by 15.04.2018 в 03:09