接收到405错误,使用表格从客户端获取数据时,不允许使用方法

问题描述 投票:-1回答:1

我正在使用Slim框架,并尝试使用一些php脚本来建立基本形式并运行。 Index.php包含一个表单,提交后将执行第二个php脚本,称为request_variable.php,该脚本将输出表单的内容。这是我的代码:

Index.php

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../../vendor/autoload.php';

$app = new \Slim\App;

$app->get('/',function(Request $request,Response $response){
$output = <<< HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="AUthor" content="tester" />
    <title> form example</title>
</head>
<body>
    <form action="request_variable.php" method="post">
    <input type="text" name="firstname" placeholder="First Name" />
    <input type="text" name"lastname" placeholder="Last Name" />
    <input type="submit" name="submit" />
    </form>
</body>
</html>
HTML;
echo $output;
});


$app->run();

request_variable.php

<?php

use Slim\Http\Request;
use Slim\Http\Response;

require '../../vendor/autoload.php';

$app = new \Slim\App;

$app->post('/',function(Request $request,Response $response) use ($app)
{
    $parameters = $request->getParsedBody();
    echo $parameters['firstname'];
    echo $parameters['lastname']; 
}

$app->run();

问题

然后测试index.php:

php -S localhost:8000 index.php

然后我在网络浏览器中输入地址。该表格是可见的,看起来还可以。我输入了我的名字和姓氏,但是,一旦我单击提交,我就会收到以下错误:

不允许使用的方法,必须为以下之一:>

作为php新手和Web应用开发新手,我想知道为什么会这样吗?我需要使用get而不是post,还是另一个问题?预先感谢。

我正在使用Slim框架,并尝试使用一些php脚本来建立基本形式并运行。 Index.php包含一种表单,提交后将执行第二个称为...

php html slim slim-3
1个回答
0
投票

使用map代替getpost

© www.soinside.com 2019 - 2024. All rights reserved.