Mordor Mordor - 7 months ago 19
PHP Question

PHP measurement converter

I'm trying to do a PHP converter that convert measurements with basic and simple code as possible (new to this). I hope this image will help you understand what I'm trying to do:

I have created a HTML file with a form that looks like this:

<form action="converter.php" method="post">
<input type="text" name="number">
<select name="choose">
<option>Choose</option>
<option value="meter" id="meter">Meter</option>
<option value="cm" id="cm">Cm</option>
<option value="foot" id="fot">Fot</option>
<option value="inches" id="tum">Tum</option>
<input type="submit" value="Convert!">
</form>


And a PHP file that look like this, and it's not much but:

<?php
$number = $_POST['number'];
$choose = $_POST['choose'];

$x = $number;
echo "$number $choose <br>";
?>


Aaaand I'm stuck. I dont really know how to convert the values, both mathematically and in code. Doesn't matter if you give me the whole code or a hint of what to do next, just need a push in the right direction.

Thank you!

Answer

Simpler one:

index.html

<form action="converter.php" method="post">
 <input type="number" name="number" required>
 <select name="choose">
        <option>Choose</option>
        <option value="meter" id="meter">Meter</option>
        <option value="cm" id="cm">Cm</option>
        <option value="foot" id="fot">Fot</option>
        <option value="inches" id="tum">Tum</option>
<input type="submit" value="Convert!">
</form>

converter.php

<?php
$number = $_POST['number'];
$choose = $_POST['choose'];

$meters = $number / ($choose == 'cm'? 100 : ($choose == 'foot'? 3.28084 : ($choose == 'inches'? 39.3701 : 1))); 

echo "Meters : $meters<br/>";
echo "Cm : ".($meters*100)."<br/>";
echo "Foot : ".($meters*3.28084)."<br/>";
echo "Inches : ".($meters*39.3701)."<br/>";
Comments