Welcome Guest, Not a member yet? Register   Sign In
Form action not working
#1

(This post was last modified: 05-29-2021, 11:54 AM by Secux.)

This is the code. When you press the submit button, nothing happens

view:
Code:
<form  method="post" action="<?php echo subSECURE;?>/index.php/User/add" autocomplete="off" enctype="multipart/form-data">
<input type="text" name="username" class="form-control" placeholder="" >
<button type="submit" class="btn btn-primary"><?= lang('Core.Dash.Add') ?></button>
</form>

Routes:
PHP Code:
$routes->post('User/add/(:segment)''User::add/$1',['filter' => 'auth']); 


Controllers:
PHP Code:
class User extends BaseController
{
    .... 
    public function add(){

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

 
// SET UP RULES
 
$rules = [
 
'username' => [
                    'rules' => 'required',
                    'errors' => [
 
'required' => lang('Core.Auth.Error.required')
                    ]
                ],
 ];

 
// VALIDATE RULES
 
if (!$this->validate($rules)) {
                // isError
 
$data['validation'] = $this->validator;
 } else {

 
// isOK
 
return redirect()->to(subSECURE .'/users');
 }
 }


 }
    ... 
Reply
#2

(This post was last modified: 05-29-2021, 12:31 PM by includebeer.)

What is subSECURE?
You're missing a route for /User/add. It's not the same thing as /User/add/(:segment).
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 05-29-2021, 01:11 PM by Secux.)

I tried with User/add and it still doesn't work

maybe I should put some kind of helper?
pressing the button does not execute the form at all

subSECURE is = secure.domain.com
in routers.php

PHP Code:
function whichSubRoute()
{
 
$subs = array("secure" => "secure/","api" => "api/");
 
$curr $_SERVER['HTTP_HOST'];
 
$curr explode('.'$curr);
 if (
array_key_exists($curr[0], $subs)) {
 return array(
$curr[0], $subs[$curr[0]]);
 }
 return 
false;
}
$choiceRoute whichSubRoute();
if (
$choiceRoute !== false) {
 if (
$choiceRoute[0] == "secure") {
 ...
$routes->match(['get''post'], 'User''User::index',['filter' => 'auth']);
 
$routes->post('User/add''User::add',['filter' => 'auth']);

 }
 if (
$choiceRoute[0] == "api") {
 }
} else {

$routes->get('/''Home::index');


Reply
#4

(05-29-2021, 01:00 PM)Secux Wrote: pressing the button does not execute the form at all

If the submit button doesn't submit the form you must have syntax error in your html or you have some javascript code that intercept the event. Look in your browser console for any error message.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

Thanks, I saw where the problem was
Reply




Theme © iAndrew 2016 - Forum software by © MyBB