Home > Enterprise >  Automatically get the error message instead of the outcome
Automatically get the error message instead of the outcome

Time:07-08

so am making a page where you can calculate all kinds of stuff but can't get the one for circles to work the way its supposed to right now it keeps saying that the input is incorrect while nothing has been selected yet and it doesn't calculate anything at all it just refreshes the page and it should be instead returning the outcome of the conversion

the code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="styling.css">
    <title>Title</title>
</head>
<body>
<header>Calculator and Converter</header>
<div >
    <a  href="#home">Home</a>
    <a href="#">TBD</a>
    <a href="#">TBD</a>
    <a href="#">TBD</a>
</div>
<content>
    <form method="POST">
        This converter is for circles
        <input type="number" name="input" placeholder="">
        <select name="circles" >
            <option value="radius">Radius</option>
            <option value="diameter">Diameter</option>
            <option value="circumference">circumference</option>
        </select>

        <select name="convercircle" >
            <option value="radius">Radius</option>
            <option value="diameter">Diameter</option>
            <option value="circumference">circumference</option>
        </select>
        <input type="submit" name="submit"  value="submit">
    </form>
</content>

</body>
</html>


<?php
$input=$_POST[''];
$circles=$_POST[''];
$convercircle=$_POST[''];
$submit=$_POST['submit'];


if (isset($_POST['submit'])) {
    if ($circles==['diameter'] && $convercircle==['radius']) {
        $output=$input*2;
        echo "The radius is $output";
    }
    elseif ($circles==['circumference'] && $convercircle==['radius']) {
        $output=$input/2;
        echo "The radius is $output";
    }
    elseif ($circles==['radius'] && $convercircle==['diameter']) {
        $output=$input*2;
        echo "The diameter is $output";
    }
    elseif ($circles==['radius'] && $convercircle==['circumference']) {
        $output=$input*2*3.14;
        echo "The circumference is $output";
    }
    elseif ($circles==['diameter'] && $convercircle==['circumference']) {
        $output=$input*3.14*2;
        echo "The circumference is $output";
    }
    elseif ($circles==['circumference'] && $convercircle==['diameter']) {
        $output=$input*3.14*2;
        echo "The diameter is $output";
    }
    else {
        echo "Please select a circle and a conversion";
    }

}



?>

CodePudding user response:

  1. You're missing the array keys when you set the variables from $_POST.
  2. When you're comparing the variables, you have [] around the strings. That puts the string in an array, but the values of the variables are just strings. Don't do that.
<?php

if (isset($_POST['submit'])) {
    $input=$_POST['input'];
    $circles=$_POST['circles'];
    $convercircle=$_POST['convercircle'];

    if ($circles=='diameter' && $convercircle=='radius') {
        $output=$input*2;
        echo "The radius is $output";
    }
    elseif ($circles=='circumference' && $convercircle=='radius') {
        $output=$input/2;
        echo "The radius is $output";
    }
    elseif ($circles=='radius' && $convercircle=='diameter') {
        $output=$input*2;
        echo "The diameter is $output";
    }
    elseif ($circles=='radius' && $convercircle=='circumference') {
        $output=$input*2*3.14;
        echo "The circumference is $output";
    }
    elseif ($circles=='diameter' && $convercircle=='circumference') {
        $output=$input*3.14*2;
        echo "The circumference is $output";
    }
    elseif ($circles=='circumference' && $convercircle=='diameter') {
        $output=$input*3.14*2;
        echo "The diameter is $output";
    }
    else {
        echo "Please select a circle and a conversion";
    }
}
  • Related