-1

I am working on a project that will take user input value from an HTML form and then convert such values from one base to the other. I have been trying for hours without success. I have created five Html input types. One for the number to be converted. Two for the base to be converted from. Three for the base to be converted to. Four, a text box for displaying the result and finally a button which the user will click to run the process. I will put the code below for warm assistance. Thanks all in advance.

I tried PHP isset method, !empty, settype, post but all did not work

<form method="POST" name="btnN"  id="chr"class="myfm" action=" <?php htmlspecialchars($_SERVER['PHP_SELF']);?>">
   <span style="color:white">From number:</span>  <br><input type="text" name="screen1" ><br><br>  
      <span style="color:white">From base:</span> <br><input type="text" name="screen2"><br><br>
       <span style="color:white">To base:</span> <br><input type="text" name="screen3"><br><br>
         <span style="color:white">Result:</span> <br><input type="text" name="screen" ><br><br>
       <input name="conver" type="button" value="Convert" onclick="btnN.screen.value='<?php echo $converted; ?>'" style="width:50%" >
   </form>  
   <?php
   $screen1=settype($_POST['screen1']);
    $screen2= settype($_POST['screen2']);
     $screen3=settype($_POST['screen3']);
     do{
       $screen1=settype($screen1,"integer");  
        $screen2=settype($screen2,"integer");
         $screen3=settype($screen3,"integer");
         $converted= base_convert($screen1,$screen2,$creen3);
         }
         while(isset($_POST['conver']));
   ?>

All that I want is, to have an HTML form with text boxes where the user can enter the number he or she wants to convert from a specified base to another specified base. example converting 12548 from base10 to base5. My main problem is with the PHP codes. Thanks for helping!

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156

2 Answers2

0

Several problems needed to be fixed:

  1. You're using settype() incorrectly. It changes the variable in place, it doesn't return the new value. You can use intval() for that. And you shouldn't convert screen1 to an integer; the first argument to base_convert must be a string (if you convert it to an integer, it will be parsed as decimal, but the whole point of this is to parse it in a specific base).
  2. You should use if, not do-while, to test if the form was submitted.
  3. You can't use the PHP variable in JavaScript the way you are. JavaScript runs on the client after the PHP script has finish. So your onclick will show the value from the previous form submission.
  4. Instead, echo the result variable into the value of the output field. This has to be after the code that does the base conversion.
  5. The button needs to be a submit button so it submits the form. If you don't want to submit the form and reload the page, you'll need to learn to use AJAX, which I'm not going to show how to do in this answer.
  6. You misspelled $screen3 as $creen3 (missing s) in your call to base_convert().
  7. You need to echo $_SERVER['PHP_SELF'] in the action attribute.
<?php
if (isset($_POST['conver'])) {
    $screen1 = $_POST['screen1'];
    $screen2 = intval($_POST['screen2']);
    $screen3 = intval($_POST['screen3']);
    $converted= base_convert($screen1,$screen2,$screen3);
} else {
    $screen1 = $screen2 = $screen3 = $converted = "";
}
?>
<form method="POST" name="btnN"  id="chr"class="myfm" action=" <?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
    <span style="color:white">From number:</span>  <br><input type="text" name="screen1" value="<?php echo $screen1; ?>" ><br><br>  
    <span style="color:white">From base:</span> <br><input type="text" name="screen2" value="<?php echo $screen2; ?>"><br><br>
    <span style="color:white">To base:</span> <br><input type="text" name="screen3" value="<?php echo $screen3; ?>"><br><br>
    <span style="color:white">Result:</span> <br><input type="text" name="screen" value="<?php echo $converted; ?>" ><br><br>
    <input name="conver" type="submit" value="Convert" style="width:50%" >
</form>  
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • There was one more really simple error in your code -- see #6. –  Aug 16 '19 at 01:19
  • @AgbesiInnocent I've tested this code, it works now. – Barmar Aug 16 '19 at 01:28
  • @Barmar O wow! it worked! thanks so so much, sir! Can you help me to prevent it from loading anytime I click the submit button? Thanks again in advance – Agbesi Innocent Aug 16 '19 at 01:43
  • Read a tutorial on AJAX. Or write the base conversion code in JavaScript. https://stackoverflow.com/questions/1337419/how-do-you-convert-numbers-between-different-bases-in-javascript – Barmar Aug 16 '19 at 18:18
0

I think you have to recreate your code, because it does confuse:

    <form method="POST" name="btnN"  id="chr"class="myfm" action=" <?php htmlspecialchars($_SERVER['PHP_SELF']);?>">
           <span style="color:white">From number:</span>  <br><input type="text" name="input" ><br><br>  
              <span style="color:white">From base:</span> <br><input type="text" name="type-from"><br><br>
               <span style="color:white">To base:</span> <br><input type="text" name="type-to">  
           <?php
        if(isset($_POST['submit'])){
           $result = base_convert(trim($_POST['input']),strtolower($_POST['type-from']),strtolower($_POST['type-to']));
        }
           ?>

    <span style="color:white">Result:</span> <br><input type="text" name="screen" value="<?php echo $result; ?>" ><br><br>
        <input name="conver" type="submit" value="Convert" style="width:50%" >
</form>
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16
  • You've introduced a ton of unnecessary complications into this code (like an exception that's thrown and immediately caught?) without resolving any of the real problems with it. –  Aug 16 '19 at 00:57
  • @duskwuff does that looks better? i just thought it is always good practice to create a `try catch` block in case if wee get any error. Isn't it? – Serghei Leonenco Aug 16 '19 at 01:02