The issue/brief intro
What I'm currently in the process of making is a simple register/login-system (which will obviously expand into more later on). I've got a controller named Users
, which features mainly two functions. One is show_register()
and one is send_register()
. This is paired with a model User_Model
, a so called Factory UserFactory
and currently (mainly for testing, these will later be combined) three views register_success
, register_fail
and show_register
. They are basically one view for saying that "Hey, congrats, you managed to register", one for saying "Oh noes, no register!" and one for showing the actual registration form.
Upon heading onto http://url.com/register, you are automatically shown the registration form itself (done through routing with $route['register'] = 'users/show_register';
).
So, once you fill out the form in the show_register
, the form action leads to the send_register
function in the Users
-controller. Input is checked, and if all is valid, it is sent to the UsersFactory
for database input. If all is well, the controller receives "true" back as a result, and shows the corresponding view. This is where our problem kicks in!
If I manage to register, my URL is now: http://url.com/public/users/send_register. The public-folder is where I keep the files, and I've realized that needs to be removed from the URL through htaccess, which I'll find my way about how to do. The issue is that I don't wish my URL to be consisting of the users-controller and the send_register function of that. I wish it to remain as http://url.com/register/, but show a different view.
What I've tried/looked into
I've looked into routing first of all. I realize (hopefully I'm right about this, otherwise I need to learn how to read) that it won't work for the issue I've described. The main reason for that is because it takes whatever I specify and sends that to a specific class/function. In this case, what we're doing is already going to the right class/function, but the "wrong" URL is shown to the user. It's also difficult since I've got different methods inside of this controller, meaning that I can't statically map all actions to lead to a single function.
I've also looked into .htaccess, but I have not found a way to actually write it. For good measure, I'll have my .htaccess file printed here on the bottom in case it would help. All I've done is modify so that the index.php-file is removed from the URL.
.htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
If there are any other files you'd like me to show, I'll gladly do so. For now, all I can think of that could be relevant is the controller itself.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
public function show_register()
{
$this->load->view("show_register");
}
public function send_register()
{
// Populate the model with the information we got from the form.
$data = array(
'user_name' => $this->input->post('username'),
'user_pass' => $this->input->post('password'),
'user_email' => $this->input->post('email'),
'user_level' => 1
);
// Then pass that on to the User Factory, which will insert that into the database.
$this->load->library("UserFactory");
if($this->userfactory->registerUser($data))
{
$this->load->view("register_success");
}
else
{
$this->load->view("register_fail");
}
}
}
?>
Apologies for it taking so long for an answer to be given!
Why not just do an ajax call?
On your view where the form is:
<div id="message_box" class="hidden"></div>
<form id="theForm" method="post" action="submitForm()"></form>
<script>
function submitForm() {
$.post("<?= site_url('Users/send_register');?>", $("#theForm").serialize(), function(data) {
success: $("#message_box").html(data).show();
error: $("#message_box").html("<p>There was an error with your request</p>").show();
});
}
</script>
There are of course a million different ways to do this. But I'm assuming those views are just message views, as in just short html success/failures (as you'd stated above). The advantage of ajax would be that the user would stay on the page, and you'd hide your url.
Hope that helps!