-1

I'm Using the Slim framework and trying to get a basic form up and running with some php scripting. Index.php contains a form that when submitted will execute a second php script called request_variable.php, which will output the contents of the form. Here is my code:

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();

The problem

I then test index.php:

php -S localhost:8000 index.php

I then enter the address in my web browser. The form is visible and appears to be ok. I enter my firstname and lastname, however as soon as i click submit i recieve the following error:

Method not allowed, must be one of:GET

As a php novice and web appication development novice i'm wondering why this is occuring? do i need to use get instead of post then or is it another issue? Thanks in advance.

digitalXmage
  • 23
  • 1
  • 7
  • Method not allowed, must be one of:GET means you should use "get" instead of "post" method in your form. I would try that first – Jonny Nov 15 '19 at 14:18
  • Move `$app->post(...)` from `request_variable.php` to `index.php` and delete `request_variable.php` file, then change form action to `/` and see if anything changes. – Nima Nov 15 '19 at 16:41
  • As stated by @Jonny, `
    ` should be `form action="request_variable.php" method="get">`
    – Alex Barker Nov 15 '19 at 21:54
  • Why is this the case tho? As in various tutorial's i've been looking up online, you can use post. And seems to have the benefit of hiding the data, according to this article: https://www.ostraining.com/blog/coding/retrieve-html-form-data-with-php/ – digitalXmage Nov 15 '19 at 23:11
  • Also what exactly is wrong with my question that it requires to be downvoted? just want to make sure for future reference. As it just makes it longer for someone to help me out, as i doubt anyone wants to answer a downvoted question. Tho it's for a genuine reason, then sure, i get it, but at least explain thanks, so i don't make the mistake in the future. – digitalXmage Nov 15 '19 at 23:16
  • You should avoid creating multiple instances of `\Slim\App` in different files. The way you're doing it, you're creating one instance in `index.php` which has one route `/` responding to a `get` request, and one instance in `request_variable.php` having one route `/`, responding to a `post` request. However, if you post a form to `request_variable.php`, your app won't know the route was actually `/`, because the `.htaccess` file (or alternative web server configuration file) tells your web server to redirect request to `index.php` not any other files. Did you try my suggestion? – Nima Nov 16 '19 at 07:49
  • Nima's point is correct. In Slim, all requests (except the assets) are tunnelled through the front controller `index.php`. – odan Nov 16 '19 at 09:18
  • Oh okay, apologies for the mistakes. Thank you for clarifying. – digitalXmage Nov 16 '19 at 13:00

1 Answers1

0

use map instead get and post:

$app->map( [ "GET", "POST" ], "/", function ( Request $request,Response $response ) use ($app) {
    if ( $request->isGet() ) {
        $body = <<< ...; // your HTML
        // point action to the route, not to file
        // <form action="/" method="post">...
        $response = $response->getBody()->write( $body );
    }
    if ( $request->isPost() ) {
        $body = $request->getParsedBody();
        $response = $response->withJson( $body );
    }
    return $response;
}

Map allows you handle same route name inside same function ( ... function( Request $req....) ). I think useful to condense my code. I used write because you're using echo. When I use a framework I try use the frameworks' tool. Often is very worth it! In my daily work, for UI routes I either return slim/php-view HTML/PHP file or return JSON object to be handled in JavaScript application (more often this way!).

( map - http://www.slimframework.com/docs/v3/objects/router.html)

( isPost and isGet - http://www.slimframework.com/docs/v3/objects/request.html)

( slim/php-view - http://www.slimframework.com/docs/v3/features/templates.html)

( body and write - http://www.slimframework.com/docs/v3/objects/request.html#the-request-body)

( withJson -> http://www.slimframework.com/docs/v3/objects/response.html )

Enrique René
  • 510
  • 1
  • 5
  • 19