Skip to main content
Participant
May 29, 2017
Answered

MethodNotAllowedError while using a POST method in PHP

  • May 29, 2017
  • 1 reply
  • 2733 views

Hi everyone!

I'm trying to get myself into web development and i'm pretty new to this so i apologize in advance for any stupid mistake haha.

I'm practicing how to create forms using PHP and i keep getting an error when i use the POST method when submitting information. Here is my first PHP file (form.php) that has the HTML content:

<html>

<body>

       <form method="post" action="form_submission.php">

            <p>Name: <input name="name" type="text" /> </p>

            <p>Size: <select name="size">

                 <option value="small">Small</option>

                 <option value="medium">Medium</option>

                 <option value ="large">Large</option>

                 </select></p>

            <p>Sex: <input type="radio" name="sex" value="female"/> Female

            <input type="radio" name="sex" value="male" /> Male</p>

  <input type="submit" name="submit" value="Submit!" />

     </form>

</body>

</html>

and here is the code for the form_submission.php file:

<?PHP

     if(isset($_POST['submit'])){

            $name = $_POST['name'];

            $size = $_POST['size'];

            $sex = $_POST['sex'];

  echo "<p>Name: $name <br> Size: $size <br> Sex: $sex </p>";

}

?>

**SIDENOTE: i have another problem with the echo statement in live view and i don't think it's processing properly; it outputs the last five characters ";}?> at the end and it doesn't register the " as the end of the echo.

When i run the form.php and fill it out i get:

{"code":"MethodNotAllowedError","message":"POST is not allowed"}

Your help is greatly appreciated and any pointers are welcomed

This topic has been closed for replies.
Correct answer osgood_

Dreamweaver doesnt have a local server. If you are testing php locally you need to download a free one like Wamp or Xampp or Mamp.

Do you have a remote server capable of running php that you can upload the files to to test them out?

1 reply

Legend
May 29, 2017

You dont need to surround the php processing code in brackets to see if the 'submit' button has been set. That really only applies if the php code is included in the same file as your form code. Its intention is to stop the php running until the 'submit' button has been pressed. As you have the php code in another file it cant run. It only runs when the form gets sent to the processing file via the forms 'action' attribute.

See if that fixes your problems

farisbAuthor
Participant
May 29, 2017

i removed it and made the code as follows:

<?PHP

            $name = $_POST['name'];

            $size = $_POST['size'];

            $sex = $_POST['sex'];

  echo "<p>Name: $name <br> Size: $size <br> Sex: $sex </p>";

?>

but i still get the same error message when i submit the form.php

i'm using Dreamweaver's local server so i don't think that's part of the problem, but looking at how simple the block of code is i don't think the flaw is in the code.

osgood_Correct answer
Legend
May 29, 2017

Dreamweaver doesnt have a local server. If you are testing php locally you need to download a free one like Wamp or Xampp or Mamp.

Do you have a remote server capable of running php that you can upload the files to to test them out?