'Form insists on GET and not POST / Codeigniter 4
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 border-0" method="post" accept-charset="utf-8">
Solution 1:[1]
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?
Solution 2:[2]
Try for button:
<input type="submit" class="btn btn-primary" name="submit" value="Save">
Controller:
if (isset($_POST['submit'])) {}
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ivanargulo |
| Solution 2 | Weyers de Lange |
| Solution 3 | rikhzan amir |
