user1032181
user1032181

Reputation: 43

How to use sites_url in codeigniter

I am facing this issue. When I try to access my test_product/import.php in my view, I will have this problem.

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

my import.php is in /view/test_product/import.php. I am opening it via my all.php in /view/test_product/all.php

Below is my code for all.php

<form id="submit_form" action="<?php echo site_url("Test_product/import");?>" method="post">
<p align="right"><button name="import" type="submit" class="btn btn-primary" id="submiting" data-loading-text="Adding ... <span class='fa fa-fw fa-spinner'></span>" form="submit_form">Import </button></p>

It is all under the same directory /view/test_product/. The purposes of this is to access to my other page import.php .

Upvotes: 0

Views: 217

Answers (3)

Hedeshy
Hedeshy

Reputation: 1386

This error is not about "site_url". it's related to Codeigniter CSRF protection. In order to fix it you should either:

  • Use the the form_open() and form_close() helper functions instead of using plain HTML for creating forms. When you use the helper function "csrf token" will be automatically inserted as a hidden field in the form.

or

  • If you can't use form_open() or form_close() you can get the CSRF token name and value using

    $this->security->get_csrf_hash();

    $this->security->get_csrf_token_name();

    and send them manually by adding the token as a hidden input field in the form.

    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

or

  • turn off the CSRF protection, which is not recommended.

Upvotes: 1

user4419336
user4419336

Reputation:

Since you said your import.php is in views then it is classed as a view file. you can not access a view file direct from site_url. You will need to create a controller to in to be able to access views

application > controllers > test_product > Import.php 

Controller For Import.php

<?php

class Import extends CI_Controller {

public function index() {
   // Form submit info goes here.
   $this->load->view('import_view');
}

}

Then you can use.

<form action="<?php echo base_url("test_product/import");?>"  method="post">

How to use URL Helpers

Form Helper

Form Validation Library

Upvotes: 1

Abdul Manan
Abdul Manan

Reputation: 2375

you are using double quotes inside double quotes.
Try
"<?php echo site_url('Test_product/import');?>"

if not loaded url_helper .try
if you want to autoload URL

file location :application/config/autoload.php
$autoload['helper'] = array('url');

if want to load per need :

$this->load->helper('url');

suggestion
it's better to autoload url_helper ,because we need it everywhere. if you want to access a resource such as css, js, image, use base_url(), otherwise, site_url().

Upvotes: 0

Related Questions