{"code":"MethodNotAllowedError","message":"POST is not allowed"} - PHP Submit Form

Community Beginner ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Hello,

I am learning to code with PHP and during my test I used the following code:

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>Untitled Document</title>

</head>

<body>

<?php

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

  // process form

  printf('User: %s

  Pass: %s

  Status: %s

  Comments: %s',

    $_POST['User'],

    $_POST['Pass'],

    $_POST['Status'],

    $_POST['Comments']);

  }

  ?>

<form method="post" action="">

  User: <input type="text" name="User"><br>

  Pass: <input type="password" name="Pass"><br>

  Status:

  <input type="radio" name="status">Single

  <input type="radio" name="status">Married

  <input type="radio" name="status">Divorced

  <input type="radio" name="status">Widowed<br>

  Comments:<textarea name="comments"></textarea><br>

  Submit:<input type="submit" name="submit" value="Submit">

  </form>

</body>

</html>

Dreamweaver didn't report any problems and when I tried to submit my form (localhost) it gave back the error on the title.

Can anyone help me figure this out?

Views

12.7K

Likes

Translate

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

Guru , Mar 31, 2017 Mar 31, 2017

Your form works, such as it is IF you change all the POST variables to lower case, including . . .

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

So here is the lesson:

Decide upon a naming convention for variable names, file names, etc. and stick with it. You've been stuck for days just because of a typo.

Likes

Translate

Translate
LEGEND ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

You have plenty wrong with both your php code and your html code. Firstly 'Status' is not the same as 'status', 'Submit' is not the same as 'submit'. You need to make sure the name attribute assigned in your form tags is the same as what you use in the php code. I would get into a habit of using ALL lower case. If you are looking for some simple entry level example coding to get you familar with php then look at the code below. The information from the form fields is assigned and stored in php variables - $user, $pass, $status, $comments - you can then use those variables to output the information to the page or do what you require to do with them, like send them in an email etc. Below they get output to the page after the submit button is clicked.

<?php

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

$user =  $_POST['user'];

$pass = $_POST['pass'];

$status =   $_POST['status'];

$comments =   $_POST['comments'];

}

?>

<form method="post" action="">

User: <input type="text" name="user"><br>

Pass: <input type="password" name="pass"><br>

Status:

<input type="radio" name="status" value="Single">Single

<input type="radio" name="status" value="Married">Married

<input type="radio" name="status" value="Divorced">Divorced

<input type="radio" name="status" value="Widowed">Widowed<br>

Comments:<textarea name="comments"></textarea><br>

Submit:<input type="submit" name="submit" value="Submit">

</form>

<?php

if(isset($user)) {

echo "<p>User: $user</p>";

}

if(isset($pass)) {

echo "<p>Pass: $pass</p>";

}

if(isset($status)) {

echo "<p>Status: $status</p>";

}

if(isset($comments)) {

echo "<p>Comments: $comments</p>";

}

?>

Likes

Translate

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 ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

I'm still getting the same error. I tried my original code with all lowercase and got the same error. I tried your code and I got the same error.

by the way, why do you have 2 different php codes? What's the function of the second one? I tried inserting it just like you wrote it but it didn't even get it as a code it just showed up on my HTML page.

Sorry, I know this may seem really simple to you but I'm barely learning and I just want to understand every single detail. I appreciate your response!

Likes

Translate

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
Adobe Community Professional ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Have you named your document with a PHP extension?

Wappler, the only real Dreamweaver alternative.

Likes

Translate

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 ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Yes.

Likes

Translate

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
Adobe Community Professional ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

And you have the server running with the document in the server's root directory?

Wappler, the only real Dreamweaver alternative.

Likes

Translate

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 ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Im only doing this locally(local host). All documents are inside the site root. I'm trying to learn PHP, so all documents I work with have php extension.

i hope that makes sense.

Likes

Translate

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
Guru ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

I disagree with ebayseller because I'm not convinced that is a PHP error.

Does the error say it is a 403 error. If so, that's the server, not PHP.

I was going to ask pretty much the same as Ben:

What webserver did you install and activate?

Is the document within the web root?

How are you running the document (http://localhost/file.php)

Create a php file, name it info.php, put in it:

<?php  phpinfo(); ?>

put it in the root, and run it. Then tell us what you get.

Likes

Translate

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 ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

I installed XAMPP, and before I began all this I created a phpinfo file and it showed me all the info.

I just ran it again and I get a blank page. Yes, all documents are on the root and I am running a local host.

Likes

Translate

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
Guru ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

I just ran it again and I get a blank page.

You ran your form and got a blank page with no error?

Likes

Translate

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 ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

No. I ran the phpinfo file and it just gave me a blank page. The submit form is still giving me the same error from the title.

Likes

Translate

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
Guru ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Then your server is not running or your path to the file is not correct

Get back to where you can run the phpinfo file and get results.

Try http://localhost/phpinfo.php (if that's what you named it)

Likes

Translate

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

Ok, I went back to the roots so to speak. Started over again and localhost/phpinfo.php is working now. I did the form.php code again (first post) and when I click submit is still doesn't return any information. There's no error at all either, it's like the button doesn't have an action linked to when I click it.

Likes

Translate

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
Guru ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

Your form works, such as it is IF you change all the POST variables to lower case, including . . .

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

So here is the lesson:

Decide upon a naming convention for variable names, file names, etc. and stick with it. You've been stuck for days just because of a typo.

Likes

Translate

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
Guru ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

Here is my naming convention:

  • never ever use capital letters anywhere unless unavoidable
  • separate words in variables with underscore and words in file names with dash
  • never use the plural form. ie: student instead of students, invoice instead of invoices
  • use descriptive variable names, even if they get long
  • use variable names in PHP that match the column names in database tables
  • Always be on the lookout for things that can be conventionalized

Likes

Translate

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

Thank you so much it finally worked!

Likes

Translate

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
New Here ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

how to do

Likes

Translate

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 ,
Dec 08, 2020 Dec 08, 2020

Copy link to clipboard

Copied

LATEST

Hi, getting the same message here, I read through this but not sure to understand everything.

 

I'm learning PHP, and here is my code:

 

<form action="infolettre.php" method="post">
<div class="form-inline pb-4">
<input type="email" name="courriel" class="form-control form-control-sm font-blu" placeholder="Ton courriel">
<input class="btn font-blu font-size-baspage rounded-0" type="submit" name="submit" value="Abonne-toi à l'infolettre">
</div>
</form>

 

Then I have an external file called infolettre.php in the same root folder as my index.html

Here is the code :

<?php
if ( isset( $_POST[ 'submit' ] ) ) {
$nom = $_POST[ 'nom' ];
$prenom = $_POST[ 'prenom' ];
$email = $_POST[ 'courriel' ];

$connection = mysqli_connect( 'localhost', 'root', '', 'courrielapp' );
if ( $connection ) {
echo "Courriel bien reçu !";
} else {
die( "connexion à la base de donnée échouée." );
}

$query = "INSERT INTO Utilisateurs(nom,prenom,courriel)";
$query .= "VALUES ('$email');

$result = mysqli_query($connection, $query);

if(!$result) {
die('tentative échouée');
}

}

?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Likes

Translate

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
Adobe Community Professional ,
Mar 28, 2017 Mar 28, 2017

Copy link to clipboard

Copied

cberlanga  wrote

Im only doing this locally(local host). All documents are inside the site root. I'm trying to learn PHP, so all documents I work with have php extension.

i hope that makes sense.


By default, your local testing server will not process form to email scripts. There are ways to make it work, but usually it's easier to just toss the files it into a test location on your remote server than it is to try to get your local server to process them.

Likes

Translate

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

I uploaded my files to a remote server and it still doesn't work. I click 'submit' and nothing happens. The page doesn't reload and there's no error either. Could it be that the php code is outdated or something of the sort?

Likes

Translate

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

I was running a local server on my Mac, and then I uploaded the files to a remote server. Still not working.

Likes

Translate

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

The code in your first post is never going to 'submit' because you dont have 'Submit' as a post attribute in the form - the name attribute for your submit button is 'submit' (note the lower case s)

'Sumbit' and 'submit' in php are completely different

Change to: (lower case 's'):

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

and change to lower case 's' and lower case 'c'

$_POST['status'],

$_POST['comments']);

Likes

Translate

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
Engaged ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

The document is obviously being processed as php or else you wouldn't see the error message, so asking if the document has a .php extension is a moot point. Also, asking if the document in question is in the server's root directory is equally moot for the reasons already mentioned. The real problem is that your local server does not have the POST method enabled as indicated by the error message that is displayed.

Likes

Translate

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 ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Do you have any idea how I can fix or see if this is the issue?

Likes

Translate

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