1

I cannot figure out what's going on. I am testing whether a form is submitted or not by this code;

if ($this->request->getMethod() === 'post') 
{
}

I have a form per below and am using Codeigniter4;

        <!-- show the form -->
        <?php
        $form = array(
        'class'       => 'form-control border-0',
        );
        ?>
        
        <?= form_open('Admin/post/post_update', $form); ?>
            
            <?= $this->include('Admin/Post/_post_form.php') ?>
            
            <!-- show buttons -->
            <div class="form-group">
                <button class="btn btn-primary">Save</button> 
            </div>
                
        </form>
        <!-- end form here -->

For some reason, when I select the submit button and I echo out
echo $this->request->getMethod(); it says 'get'. I was expecting to read 'post'.

I read the documentation but cannot see anywhere where I can state make the submit a post instead of get.

When I view the html, it looks correct though as follows;

 <form action="https://development.example.com/Admin/post/post_create" class="form-control&#x20;border-0" method="post" accept-charset="utf-8">
spreaderman
  • 918
  • 2
  • 10
  • 39
  • Have you checked the rendered HTML? – Don't Panic May 24 '21 at 08:45
  • Does `Admin/Post/_post_form.php` include another `
    `? Does your browser's devtools show the request as POST or GET?
    – Don't Panic May 24 '21 at 10:19
  • No
    included in _post_form.php
    – spreaderman May 25 '21 at 02:54
  • You still didn't confirm what your browser's devtools shows. If it shows a GET, then this is a simple HTML problem, and you just need to strip your form down until you work out what is going on. The code you've shown should not cause what you're describing, so more than likely there is something else involved which you're not seeing - eg Javascript? Some CSS causing an `` to be overlaid on your button? – Don't Panic May 28 '21 at 07:29
  • @don'tpanic I cannot get devtools to show the headers. Tried this https://stackoverflow.com/questions/15603561/how-can-i-debug-a-http-post-in-chrome. Seems to me that CI4 is running validation right after the form is posted. If validation is run, it will produce a GET ... Another strange this is when I dd($this->request->getPost()); there is nothing in it. This is really confusing. – spreaderman May 29 '21 at 00:35
  • On the network tab, make sure "Preserver Log" is ticked, then you will see all requests. If there's a redirect happening you should see it as a seperate request after the initial one. Try simplifying, remove everything from the controller method *except* [`$request = service('request'); echo $request->getMethod();`](https://codeigniter.com/user_guide/concepts/http.html). I've only used CI3 so not familiar with CI4 - is it possible the validation is being applied at the controller level, or the route, rather than at the individual method? – Don't Panic May 29 '21 at 08:19

3 Answers3

0

Change the button and use <button type="submit" class="btn btn-primary">Save</button>

Also, as the previous user said, check if method="post" is present in the HTML.

Finally, can you use $_SERVER['REQUEST_METHOD'] instead of getMethod() and post the result?

ivanargulo
  • 326
  • 1
  • 5
  • * tried but same thing. * Ref html, in the initial question, I have included the html and post is mentioned. * $_SERVER['REQUEST_METHOD'] replies GET scratching head now. – spreaderman May 25 '21 at 02:54
0

Try for button:

<input type="submit" class="btn btn-primary" name="submit" value="Save">

Controller:

if (isset($_POST['submit'])) {}
Weyers de Lange
  • 280
  • 2
  • 11
  • Same thing with and if (isset($_POST['submit'])) {} – spreaderman May 27 '21 at 03:38
  • I am doing validation in the MODEL. I am new to CI4. There are errors on the form and assume that produces a GET. But why would that supersede if ($this->request->getMethod() === 'post'){} – spreaderman May 27 '21 at 03:56
0

I am also facing the same problem as yours, $request->getMethod() is says 'GET' and it should be 'POST'.

Code below works for me. Its return 'post' method on controller.

View:

> <?php echo form_open(base_url('/admin/post/post_update')  ,
> array('class' => 'form-horizontal form-groups-bordered validate',
> 'enctype' => 'multipart/form-data', 'method' => 'post' ));?>

Controller:

 public function post($param1 = NULL) { if($param1 == 'post_update') { $resp = $this->request->getMethod(true); return redirect()->to('/admin/post/'. $resp );}}

How to test getMethod(). The redirect url will shows :
https://www.{domain}.com/admin/post/POST