-3

I am using a simple PHP & HTML script which lets the user type a text, and the script converts the input to binary. When opening the script however, I see this in the input box:

Image attached And nothing happens when I'm pressing the submit button. Can somebody help me solve the issue? code is provided below.

<?php
// Initialize the input and binary variables
$input = '';
$binary = '';

// Check if the form has been submitted
if(isset($_POST['submit'])) {
    $input = $_POST['input'];

    // Convert the input text to binary
    for ($i = 0; $i < strlen($input); $i++) {
        $binary .= decbin(ord($input[$i])) . ' ';
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Text to Binary Converter</title>
</head>
<body>
    <form method="post">
        <label for="input">Enter text to convert:</label><br>
        <textarea id="input" name="input" rows="4" cols="50"><?php echo $input; ?></textarea><br>
        <input type="submit" name="submit" value="Convert">
    </form>

    <?php if(!empty($binary)) { ?>
        <p>
            Binary conversion of "<?php echo $input; ?>":<br>
            <?php echo $binary; ?>
        </p>
    <?php } ?>
</body>
</html>

I tried changing some code but nothing worked, there is always the same thing.

riots19
  • 1
  • 2

1 Answers1

0

The code itself is correct. From my experience, this is usually caused by the file extension not being set to '.php', causing the script to not parse correctly. Can you please check that the script's file extension is set to '.php' rather than, say, '.html'?

Alon Alush
  • 719
  • 3
  • 15
  • Yes you are right, The script extension was indeed .html i changed it and the script worked perfectly! thanks for the great answer! – riots19 Mar 05 '23 at 19:01