I am trying to get my login/registration form to submit. I suspected it was because I did not have post routes set, but even after adding these it still leads to the same page. I am able to access the pages directly, for example localhost:8080/SignUpController/store but can't seem to get the forms to submit to these pages. Here's the code:
Sign up page
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Sign Up</title>
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-md-center">
<div class="col-5">
<h2>Register User</h2>
<?php if(isset($validation)):?>
<div class="alert alert-warning">
<?= $validation->listErrors() ?>
</div>
<?php endif;?>
<form action="<?php echo base_url(); ?>/SignupController/store" method="post">
<div class="form-group mb-3">
<input type="text" name="name" placeholder="Name" value="<?= set_value('name') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="password" placeholder="Password" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="confirmpassword" placeholder="Confirm Password" class="form-control" >
</div>
<div class="d-grid">
<button type="submit" class="btn btn-dark">Signup</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Sign in page
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Sign In</title>
</head>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-5">
<h2>Login</h2>
<?php if(session()->getFlashdata('msg')):?>
<div class="alert alert-warning">
<?= session()->getFlashdata('msg') ?>
</div>
<?php endif;?>
<form action="<?php echo base_url(); ?>/SigninController/loginAuth" method="post">
<div class="form-group mb-3">
<input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="password" placeholder="Password" class="form-control" >
</div>
<div class="d-grid">
<button type="submit" class="btn btn-success">Sign in</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Sign in Controller
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class SignInController extends Controller
{
public function index()
{
helper(['form']);
echo view('signin');
}
public function loginAuth()
{
$session = session();
$userModel = new UserModel();
$email = $this->request->getVar('email');
$password = $this->request->getVar('password');
$data = $userModel->where('email', $email)->first();
if($data){
$pass = $data['password'];
$authenticatePassword = password_verify($password, $pass);
if($authenticatePassword){
$ses_data = [
'id' => $data['id'],
'name' => $data['name'],
'email' => $data['email'],
'isLoggedIn' => TRUE
];
$session->set($ses_data);
return redirect()->to('/profile');
}else{
$session->setFlashdata('msg', 'Password is incorrect.');
return redirect()->to('/signin');
}
}else{
$session->setFlashdata('msg', 'Email does not exist.');
return redirect()->to('/signin');
}
}
}
Sign Up Controller
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class SignUpController extends Controller
{
public function index()
{
helper(['form']);
$data = [];
echo view('signup', $data);
}
public function store()
{
helper(['form']);
$rules = [
'name' => 'required|min_length[2]|max_length[50]',
'email' => 'required|min_length[4]|max_length[100]|valid_email|is_unique[users.email]',
'password' => 'required|min_length[4]|max_length[50]',
'confirmpassword' => 'matches[password]'
];
if($this->validate($rules)){
$userModel = new UserModel();
$data = [
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
'password' => password_hash($this->request->getVar('password'), PASSWORD_DEFAULT)
];
$userModel->save($data);
return redirect()->to('signin');
}else{
$data['validation'] = $this->validator;
echo view('signup', $data);
}
}
}
Routes
$routes->get('/', 'SignupController::index');
$routes->post('/', 'SignupController::store');
$routes->get('/signup', 'SignUpController::index');
$routes->post('/signup', 'SignUpController::store');
$routes->get('/signin', 'SignInController::index');
$routes->post('/signin', 'SignInController::loginAuth');
Prior to this I was having a problem where after clicking log in or sign up it would lead me to a page such as http://localhost/Ci4/index.php/signin which would give me a 404 page not found. Changing the baseURL back to the default http://localhost:8080/ (which is what I'd prefer to use) did not seem to solve that issue so after some research it seems that moving the index.php and .htaccess files from the public folder to the project's root has solved that issue. Now I'm at the present issue where the login and sign up buttons don't seem to do anything. I'm not sure if this is relevant to the issue but thought I might add this info.
Any help with this or pointing me in the right direction would be greatly appreciated. I am also using XAMPP as my web server.
You have set your routes as "/signup" and "/signin". On your form actions you need to use these uris, so change as follows
action="<?php echo base_url(); ?>/SignupController/store"
to
action="<?php echo base_url(); ?>/signup"
and
action="<?php echo base_url(); ?>/SigninController/loginAuth"
to
action="<?php echo base_url(); ?>/signin"