Hello I have a simple page that allows a user to enter a binary value which is then converted into a decimal value. However, I am using two fields, one for entering the binary value, and the other to display the result in decimal.
I want to only use on field, so when the user enters the binary value, the result in decimal will appear in the same box that they entered input. Here is my current code:
<?php
if (isset($_POST['submit']))
{
$str = $_POST['binToDec'];
$pos = 0;
$sum = 0;
$tempSum = 0;
$strLength = strlen($_POST['binToDec']) - 1;
$powerOfTwo = 1;
if ( $str{strLength} == 1 )
$sum += 1;
for ( $i = $strLength - 1; $i >=0; $i-- )
{
$tempSum = ($powerOfTwo *= 2);
if ( $str{$i} == 1 )
$sum += $tempSum;
}
}
?>
<html><body>
<form method="post" action="#">Binary value: <input name="binToDec"><br />
Result: <input value="<?php if (isset($sum)) echo $sum ?>"><br />
<input type="submit" name="submit" value="Convert">
</form>
<body></html>