1

Hi I am a beginner to programming and have just started learning PHP and wanted to run some experiments with it on my own on visual studio code but when I run the program on live server, it gives me an error can anybody help me?

 <html>
<head>
</head>
<body>
    <form action="thing.php" method="POST">
        <label for="firstname">First Name:</label>
        <input type="text" id="firstname" name="firstname" /><br />
        <input type="submit" value="Report Abduction" name="submit">
    </form>
</body>
</html>

and in thing.php i wrote

<html>
<head>
</head>
<body>
<h2>Testing</h2>
    <?php
    $first_name=$_POST['firstname'];
    echo 'Your first name is '. $first_name;
    ?>
</body>
</html>

The error i received is This page isn’t working If the problem continues, contact the site owner. HTTP ERROR 405

Bop
  • 63
  • 1
  • 5
  • 2
    “An error” is not a useful description of the problem. Try quoting the actual error message. – Quentin Sep 06 '20 at 23:38
  • Yes, sorry I am new to stack overflow, i edited my question to quote the error – Bop Sep 06 '20 at 23:49
  • What is _"live server"_? Is that some VS Code extension? – Phil Sep 07 '20 at 00:24
  • In order to run PHP code, you need a server capable of doing so. PHP comes with one pre-installed so provided you actually [installed PHP](https://www.php.net/manual/install.php), from your VSCode terminal, try running `php -S localhost:8000` then open your browser to `http://localhost:8000/` – Phil Sep 07 '20 at 00:29
  • Yes live server is a VS code extension – Bop Sep 07 '20 at 00:29
  • I take it you haven't seen this note then ~ https://github.com/ritwickdey/live-server-web-extension#the-common-misconception – Phil Sep 07 '20 at 00:30
  • I would just recommend on using Jetbrains PHPstorm. but its a paid product – Bamuel Sep 07 '20 at 01:06

1 Answers1

2

HTTP 405 means “Method not allowed”. In this case, it is the POST request that is not allowed.

The VS Code Live Server extension (not to be confused with the browser extension that Phil linked to in the comments) only supports static files. It doesn’t support PHP.

Since it doesn’t support any kind of server-side programming, it doesn’t make sense to make a POST request do it, so it throws an error when you do.

You need to use a web server which supports PHP instead.

PHP has a built-in development server which is quick to get up and running. You might also consider using the same server that your production environment would use to minimise differences between your development environment and other environments your code will run in.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I only linked it because it mentions both the browser add-on and the extension. "_Neither the browser add-on **nor the VS Code extension** will host a server for: PHP, .NET or NodeJS"_ – Phil Sep 07 '20 at 11:07