-1

I'm making a form as an IT homework and I would like to make it working when submitted, validating the data and so on. Firstly, I made a form.html file where there was the form which, when submitted, redirects to the form.php file that got the data. It couldn't work due to the error of the title up here. After that, I completely copied and pasted an entire php file code from w3schools and I adapted it with my form.html (simply, I replaced the html code in that file with mine and I didn't touch the php code). Well, now this file gets downloaded but not executed. What the hell?

Here form.html

<form method="post" action='form.php'>
    <label for="fname"><b>First name</b></label><br>
    <input type="text" placeholder="Enter first name" name="fname" required><br><br>

    <label for="lname"><b>Last name</b></label><br>
    <input type="text" placeholder="Enter last name" name="lname" required><br><br>

    <label for="email"><b>Email</b></label><br>
    <input type="text" placeholder="Enter email" name="email" required><br><br>

    <label for="password"><b>Password</b></label><br>
    <input type="password" placeholder="Enter password" name="password" required><br><br>

    <button type="submit">Submit</button>
    <label id="checkbox-label">
    <input type="checkbox" checked="checked" name="remember">
        Remember me
    </label><br>

    <span id="forgot-pwd"><a href="#">Hai dimenticato la password?</a></span>
</form> 

Here form.php, the old file php, the one that gives me the error 405

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Form Validation</title>
    </head>
    <body>
        <?php 
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            function secure_input($input) {
                return htmlspecialchars(stripslashes(trim($input)));
            }

            $fname = $lname = $email = $password = "";
            $remember = $_POST['remember'];
            
            $fname = secure_input($_POST["fname"]);
            $lname = secure_input($_POST["lname"]);
            $email = secure_input($_POST["email"]);
            $password = secure_input($_POST["password"]);

            echo $_POST;
        }
        
        ?>

    </body>
</html>

Now, the hybrid file php adapted with my form:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Form</title>
    </head>
    <body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = test_input($_POST["name"]);
    $email = test_input($_POST["email"]);
    $website = test_input($_POST["website"]);
    $comment = test_input($_POST["comment"]);
    $gender = test_input($_POST["gender"]);
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>
        <form method="post" action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>
            <label for="fname"><b>First name</b></label><br>
            <input type="text" placeholder="Enter first name" name="fname" required><br><br>

            <label for="lname"><b>Last name</b></label><br>
            <input type="text" placeholder="Enter last name" name="lname" required><br><br>

            <label for="email"><b>Email</b></label><br>
            <input type="text" placeholder="Enter email" name="email" required><br><br>

            <label for="password"><b>Password</b></label><br>
            <input type="password" placeholder="Enter password" name="password" required><br><br>

            <button type="submit">Submit</button>
            <label id="checkbox-label">
            <input type="checkbox" checked="checked" name="remember">
                 Remember me
            </label><br>

            <span id="forgot-pwd"><a href="#">Hai dimenticato la password?</a></span>
        </form> 
    </body>
</html>

I really don't know what I am doing wrong, I have been here for hours trying to solve this. Thanks to who will answer <3

Ah, I almost forgot, I'm programming in VSCode, using the extension Live Server, an extension that creates a local server, more or less like XAMPP does, if you know that. I tried also to run the first php file in my Github Page at form.html (then form.php) but it gives the 405 error too.

Seintian
  • 23
  • 8
  • Pretty sure the vanilla Live Server doesn't support PHP. You'd need to install a Live Server extension to support it. Edit: says it right there on https://ritwickdey.github.io/vscode-live-server/ "_Live Server for server side pages like PHP. Check Here_" – brombeer May 15 '21 at 19:52
  • Have you tried checking any log files? I think this might help you find the problem. – MBiabanpour May 15 '21 at 19:54
  • log files? where can I find them using live server extension in vscode? - later I'll look for that link, thank you brombeer – Seintian May 15 '21 at 19:57

0 Answers0