Search code examples
formscodeigniter-4

Codeigniter 4 form action path


sorry for the very noob question. I have a page : www.example.com/edit/3 with form inside.

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

Usually my form action I will specify something like add, register, etc. In this case, I am not sure how do I specify it. Currently it is working when I save it if I leave it blank without specifying anything. But I am not sure is this the right way to do it.

Controller

public function edit_game(){}

Routes

$routes->match(['get', 'post'], 'account/edit_game/(:num)', 'Addgame::edit_game/$1');

Can anyone advise?


Solution

  • action="" is the URI to which send the form data, aka the controller where you treat the received data. If not specified, it will be the same page as the one you are currently on.

    What you can do:

    <form method="post">
    </form>
    
    <form method="post" action=""> <!-- same as previous -->
    </form>
    
    <form method="post" action="/some/page">
    </form>
    

    In the last example, note the leading /, it means it is according to the site root. If you forget it, it will be relative to the current URI.

    Exemple, if the page containing the <form> is http://example.com/edit/3:

    • action="/some/page" will redirect to http://example.com/some/page.
    • action="some/page" will redirect to http://example.com/edit/some/page