1

I want to know if it is possible for me to access my 'new_admin' function inside my 'admin' controller without new_admin begin added to the link.

My controller:

<?php
class Admin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('Admin_model', '', TRUE);
        $this->load->view('admin/admin_nav');
    }

    public function index()
    {
        if($this->session->userdata('logged_in'))
        {
            $this->load->view('admin/admin_dashboard');
        } 
        else 
        {
            redirect('login');
        }
    }

    public function new_admin()
    {
        $data = [];

        if($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $email = $this->input->post('email');

            $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');

            if($this->form_validation->run())
            {
                $result = $this->Admin_model->check_if_admin($email);

                if(!$result)
                {
                    $new_admin = new Admin_model();
                    $new_admin->email = $email;
                    $new_admin->password = password_hash('admin', PASSWORD_DEFAULT, ['cost' => 12]);
                    $this->Admin_model->add_new_admin($new_admin);
                    $data['success'] = 'success';
                }
                else
                {
                    $data["error_email"] = 'error';
                }
            }
        }

        $this->load->view("admin/admin_dashboard", $data);
    }
}

My view:

<div>
    <div>
        <h1>Dashboard</h1>
    </div>

    <div>
        <div>Add a new admin</div>
        <div>
            <form action="<?php echo site_url('admin/new_admin') ?>" method="post">
                <label for="email">Email new admin</label>
                <input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>">
                <?php echo form_error('email', '<div>', '</div>'); ?>

                <?php if (isset($succes)): ?>
                <p><?php echo $succes; ?></p>
                <?php endif ?>

                <?php if (isset($error_email)): ?>
                <p><?php echo $error_email; ?></p>
                <?php endif ?>

                <button type="submit">Add new admin</button>
            </form>
        </div>
    </div>
</div>

This snippet works, but I would like for it to work without the url changing. Is this possible?

Right now the url I first get to is: 'example.com/admin'
And it becomes 'example.com/admin/new_admin' after form submit.

Thanks in advance.

1 Answer 1

1

You can change the form action url to index function and detect it if it comes from a submit action.Here is some idea

Change your form action URL(inside view file) like this

<form action="<?php echo site_url('admin') ?>" method="post">

Now change your Controllers index function like this

public function index()
{
    if($this->input->post())
    {
        $this->new_admin();
    }
    else
    {
        if($this->session->userdata('logged_in'))
        {
            $this->load->view('admin/admin_dashboard');
        }
        else
        {
            redirect('login');
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.