Home > database >  PHP Codeigniter giving error: An Error Was Encountered The action you have requested is not allowed
PHP Codeigniter giving error: An Error Was Encountered The action you have requested is not allowed

Time:04-15

I am getting the following error when trying to add a new function to my existing product. I am new to php codeIgniter and have been stuck on this for a while. Any help will be appreciated and thanks in advance. If any further file is needed, let me know and I will update the post. When I try to submit the search_add_view form, I get the following error:

Error Was Encountered The action you have requested is not allowed.

my profile controller profile.php is:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('download');
        $this->load->library('form_validation');
        $this->load->library('table');
        $this->load->model('searches_model');
        $this->searches = $this->searches_model->get_searches();
        $this->sites = $this->searches_model->get_sites();
        $this->load->driver('scraper', array_column($this->sites, 'driver'));
        //check auth
        if (!is_admin() && !is_user()) {
            redirect(base_url());
        }
    }


    public function index()
    {
        if (user()->qr_code == '') {
            $this->generate_qucode(user()->slug);
        }
        $data = array();
        $data['page_title'] = 'Profile';
        $data['user'] = $this->admin_model->get_user_info();
        $data['payment'] = $this->admin_model->get_my_payment();
        $data['features'] = $this->admin_model->active_features($data['payment']->package_id);
        $data['fonts'] = $this->admin_model->select('google_fonts');
        $data['countries'] = $this->admin_model->select('country');
        $data['main_content'] = $this->load->view('admin/user/profile', $data, TRUE);
        $this->load->view('admin/index', $data);
    }

    public function search_index()
    {
        $this->form_validation->set_rules('search[name]', 'Search Name', 'required');
        $this->form_validation->set_rules('search[site_id]', 'Site', 'required');
        $this->form_validation->set_rules('search[url]', 'Search URL', 'required');
        if ($this->form_validation->run())
        {
            $data = $this->input->post('search', TRUE);
            $search_id = $this->searches_model->add_search($data);
            redirect();
        }
        $data = array(
            'subview' => 'search_add_view',
            'sites' => $this->sites,
            'searches' => $this->searches
        );

        $data['page_title'] = 'Search View';
        $data['subview'] = $this->load->view('admin/scraper/search_add_view', $data, TRUE);
        $data['sites'] = $this->sites;
        $data['searches'] = $this->searches;
        $data['main_content'] = $this->load->view('admin/scraper/searches_view', $data, TRUE);
        $this->load->view('admin/index', $data);

    }

    public function edit($search_id)
    {
        $search = $this->searches_model->get_search($search_id);
        $this->form_validation->set_rules('search[name]', 'Search Name', 'required');
        $this->form_validation->set_rules('search[site_id]', 'Site', 'required');
        $this->form_validation->set_rules('search[url]', 'Search URL', 'required');
        if ($this->form_validation->run())
        {
            $data = $this->input->post('search', TRUE);
            $this->searches_model->update_search($search_id, $data);
            redirect();
        }
        $data = array(
            'subview' => 'search_edit_view',
            'sites' => $this->sites,
            'searches' => $this->searches,
            'search' => $search
        );
        $data['main_content'] = $this->load->view('admin/scraper/search_edit_view', $data, TRUE);
        $this->load->view('admin/index', $data);
    }

/**
 * Delete a search
 *
 * @param integer search id to delete
 * @return none
 */
    public function delete($search_id)
    {
        $search = $this->searches_model->get_search($search_id);
        $search['id'] = $search_id;
        if ($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $data = array('id' => $search_id);
            $this->searches_model->delete_search($data);
            redirect();
        }
        $data = array(
            'subview' => 'search_delete_view',
            'sites' => $this->sites,
            'searches' => $this->searches,
            'search' => $search
        );
         $data['main_content'] = $this->load->view('admin/scraper/search_delete_view', $data, TRUE);
        $this->load->view('admin/index', $data);
    }
    

    public function execute($search_id)
    {
        $search = $this->searches_model->get_search($search_id);
        $this->form_validation->set_rules('search[url]', 'Search URL', 'required');
        if ($this->form_validation->run())
        {
            $search['url'] = $this->input->post('search[url]', true);
            $output = $this->scraper->scrape($search['url'], $search['driver']);
            force_download($search['name'] . '.csv', $output);
        }
        $data = array(
            'subview' => 'search_execute_view',
            'sites' => $this->sites,
            'searches' => $this->searches,
            'search' => $search
        );
         $data['main_content'] = $this->load->view('admin/scraper/search_execute_view', $data, TRUE);
        $this->load->view('admin/index', $data);
    }

}

my routes.php is:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'home/index';
$route['404_override'] = 'home/error_404';
$route['translate_uri_dashes'] = FALSE;


$route['index'] = 'home/index';
$route['error-404'] = 'home/error_404';

$uri = $this->uri->segment(1);
if ( $uri == 'searches_view' || $uri == 'search_add_view' || $uri == 'search_execute_view' || $uri == 'search_delete_view' || $uri == 'search_edit_view') {

 
    $route['search_add_view/(:any)'] = 'admin/profile/search_add_view/$1';
    $route['searches_view'] = 'admin/profile/search_index';

    $route['search_execute_view'] = 'admin/profile/search_execute_view';
    $route['search_delete_view'] = 'admin/profile/search_delete_view';
    $route['search_edit_view'] = 'admin/profile/search_edit_view';

}

search_view.php is:

<?php include'include/header.php';?>
<?php include'include/left_sideber.php';?>

    <div style="height:100vh; margin-left: 230px;" >

    <?php echo $subview;?>

    <h2>Saved Searches</h2>

<?php
    if (count($searches) == 0)
    {
        echo '<p>There are no saved searches.</p>';
    }
    else
    {
        $this->table->set_heading('Search Name', 'Site', 'Search URL', 'Edit', 'Delete', 'Execute');
        foreach ($searches as $search)
        {
            $this->table->add_row(
                $search['name'], $search['site_name'], $search['url'],
                anchor('searches/edit/' . $search['id'], 'Edit'), 
                anchor('searches/delete/' . $search['id'], 'Delete'),
                anchor('searches/execute/' . $search['id'], 'Execute')
            );
        }
        echo $this->table->generate();
    }
?>
  <?php echo $main_content;?>
<?php include'include/footer.php';?>
search_add_view.php:

  <div >
        <form method="post">
        
          <fieldset>
    
            <legend>Add a New Search</legend>
              
            <p >
              <label for="name">Search Name</label>
              <input type="text" id="name" name="search[name]" value="<?php echo set_value('search[name]', ''); ?>" size="32" maxlength="32" autofocus>
    <?php echo form_error('search[name]', '          <br>', '<br>' . PHP_EOL); ?>
            </p>
    
            <p>
              <label for="sites">Site</label>
              <select id="sites" name="search[site_id]">
                <option value="" selected>Select one</option>
    <?php foreach ($sites as $row):?>
                <option value="<?=$row['id']?>" <?php echo set_select('search[site_id]', $row['id']); ?>><?=$row['name']?></option>
    <?php endforeach;?>
              </select>
    <?php echo form_error('search[site_id]', '          <br>', '<br>' . PHP_EOL); ?>
            </p>
            
            <p>
              <label for="url">Search URL</label>
              <input type="text" id="url" name="search[url]" value="<?php echo set_value('search[url]', ''); ?>" size="105" maxlength="254">
    <?php echo form_error('search[url]', '          <br>', '<br>' . PHP_EOL); ?>
            </p>
    
            <p><input type="submit" name="submit" value="Ok"></p>
            
          </fieldset>
    
        </form>
    
        <p>&nbsp;</p>
</div>

search_model.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Searches_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_searches()
    {
        return $this->db->query(
'SELECT
    searches.id AS id,
    searches.name AS name,
    searches.url AS url,
    sites.id AS site_id,
    sites.name AS site_name,
    sites.driver AS driver
FROM searches
INNER JOIN sites ON
    searches.site_id = sites.id
;'
        )->result_array();
    }

    public function get_search($search_id)
    {
        $query = $this->db->query(
'SELECT
    searches.name AS name,
    searches.url AS url,
    sites.id AS site_id,
    sites.name AS site_name,
    sites.driver AS driver
FROM searches
INNER JOIN sites ON
    searches.site_id = sites.id
WHERE searches.id = ' . $search_id . '
;'
        )->result_array();
        return $query[0];
    }
    
    public function add_search($data)
    {
        $this->db->insert('searches', $data);
        return $this->db->insert_id();
    }
    
    public function update_search($id, $data)
    {
        $this->db->update('searches', $data, array('id' => $id));
    }

    public function delete_search($data)
    {
        $this->db->delete('searches', $data);
    }

    public function get_sites()
    {
        return $this->db->query(
'SELECT
    sites.id AS id,
    sites.name AS name,
    sites.driver AS driver
FROM sites
;'
        )->result_array();
    }
    
}

my file structure is: enter image description here enter image description here

CodePudding user response:

You might as well take a look at this similar question -- The action you have requested is not allowed. Codeigniter

Look at this specific answer - might help!

  • Related