Search code examples
phphtmlvisual-studio-codehttp-status-code-405

How to run PHP with vscode


I'm trying to create a simple HTML form that redirects users to a thank you message displayed by the server, and written in PHP. However, nothing I do seems to work. I get a 405 every time I try to run the code. If I refresh the page it starts downloading. I'm using the live server extension and I have PHP installed.

HTML file:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel = "stylesheet" type = "text/css" href = "styles-signup.css">
    <script type = "text/javascript" src = "script-signup.js"></script>

</head>
<body>
    <div>
        <h1 class = "title"></h1>
    </div>
    <div class = "form">
        <h1 id = "header">Create Your Account</h1>
        <form method = "POST" id = "signup" action = "action.php">
            <input name = "firstName" type = "text" placeholder="First Name" required id = "fname">
            <input name = "lastName" type = "text" placeholder = "Last Name" required id = "lname">
            <input name = "Email" type = "email" placeholder="email, e.g. [email protected]" required id = "mail">
            <input name = "newPassword" type = "password" id = "password" placeholder="Password" required minlength="8" maxlength="25">
            <input name = "passwordConfirm" type = "password" placeholder = "Confirm Password" required minlength="8" maxlength="25" id = "cpassword">
           <br>

           <input name = "gender" type = "radio" id = "male" required>
           <label for = "gender" id = "mlabel">Male</label>

           <input name = "gender" type = "radio" id = "female" required>
           <label for = "gender" id = "flabel">Female</label>

           <input name = "gender" type = "radio" id = "other" required>
           <label for = "gender" id = "olabel">Other</label>
           <br>

           <br>
           <input name = "birthdate" type = "number" min = "1920" max = "2019" placeholder="Year Of Birth" id = bd required>
           <br>

            <input name = "terms" type ="checkbox" id = "box" required>

            <p id = "tc">I agree to the <a href = "termsandconditions.txt" id = "tcLink">Terms and Conditions</a></p>

            <input name = "submit" type = "Submit" id = "button" value = "Sign up">

            <a href = "login.html" id = "login">Already have an account? Log in</a>

        </form>
    </div>

</body>
</html>

PHP File:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <?php
    echo "<p>Hi</p>";
    ?>

</body>
</html>

I get this error:

Error:


Solution

  • There is an Issue with this Question


    VS Code is not a server, it is a highly specialized editor. In a nushell, this means that you cannot use it to accept HTTP request, and then preprocess those request using an executable. The reason that is a problem is because, PHP will not just work without being able to run its executable, but at its very core, the very essence of what PHP is, is an executable (with some libraries included).

    SO I Can't use VS Code to write PHP?

    Well, you can, but you will need to set up a development server that you will send requests to from VSCode. In other words, when you execute a PHP document in the browser, what the browser does, is it sends that script to your server, your server preprocesses the php document, and responds with an HTML document that was dynamically created using your initial PHP script. So, you will need a server (we call these development servers) that you can send your code to while it is in development, so you can see what your are creating, as you write it. There are extensions offered in the Visual Studio Marketplace that can help. Currently the most popular extension is a GUI server called...

    Open PHP/HTML/JS In Browser

    The link above takes you to the extensions page in the VS-Marketplace, but it's easiest to just install it from inside of VS Code using VS Code's in-editor extension view.

    You will also have to have PHP installed correctly, which is beyond the scope of this answer, but if you need help with that, this is a really great guide that covers installing PHP on any platform, and any server.