Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Locked
1

MethodNotAllowedError while using a POST method in PHP

Community Beginner ,
May 29, 2017 May 29, 2017

Copy link to clipboard

Copied

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

Views

2.6K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 29, 2017 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?

Votes

Translate
LEGEND ,
May 29, 2017 May 29, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 29, 2017 May 29, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 29, 2017 May 29, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 29, 2017 May 29, 2017

Copy link to clipboard

Copied

I already had MAMP set up but didnt realize that i need to save my php files in the htdocs directory, rookie mistake haha i moved the files and that fixed both issues (the error and the echo).

thanks a lot for your help!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 29, 2017 May 29, 2017

Copy link to clipboard

Copied

LATEST

Ok have fun, php opens up a whole new world.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines