Welcome Guest, Not a member yet? Register   Sign In
Ajax doesn't work after using _remap
#1

Hi
I have added _remap() function in controller because I need to pass parameter in index, also index should work without passing parameter inside.
Example with passed parameter in url: mywebsite.com/history/12
Example without parameter in url: mywebsite.com/history
Both examples are working with _remap() function in controller !

Now issue that I have is that when I want to use ajax and call function in same controller it doesn't work, I get returned whole page content, but when I remove _remap() from controller then this examples mentioned above is not working, but ajax working. How this can be fixed ?

Controller

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

class 
History extends MY_Customcontroller {

    function 
__construct()
    {
        
parent::__construct();
    }

    public function 
_remap($id)
    {
        
$this->index($id);
    }

    public function 
index($id null)
    {
        
$this->load->view('templates/header');
        if (
$id == null) {
            
// error, load other view
        
} else {
            
$this->load->view('history');
        }
        
$this->load->view('templates/footer');
    }

    
// Get history list through ajax
    
public function get_history($id)
    {
        
$data = array();

        
$mem_data $this->orders_model->make_datatables();

        foreach (
$mem_data as $order) {
            
$data[] = array(
                
$order->ticket_status,
                
$order->symbol != '' && strlen($order->symbol) > substr($order->symbol06) : $order->symbol,
                
$order->order_type,
                
date('Y-m-d H:i'strtotime($order->open_time)),
                
number_format($order->open_price2'.'','),
                
number_format($order->lot_size2'.'','),
                
number_format($order->stop_loss2'.'','),
                
number_format($order->take_profit2'.'','),
                
$order->close_time == '1970-01-01 00:00:00' 'N/A' date('Y-m-d H:i'strtotime($order->close_time)),
                
number_format($order->close_price2'.'','),
                
number_format($order->profit2'.'',')
            );
        }

        
$output = array(
            
'draw' => $_POST['draw'],
            
'recordsTotal' => $this->orders_model->get_all_data(),
            
'recordsFiltered' => $this->orders_model->get_filtered_data($_POST),
            
'data' => $data
        
);

        echo 
json_encode($output);
    }


View

PHP Code:
<script>
var 
user_id '<?php echo $this->uri->segment('2'); ?>';

$.
ajax({
    url'<?php echo base_url('history/get_history/'); ?>'+user_id,
    type'post',
    success: function(response) {
        console.log(response)
    }
});
</
script
Reply
#2

(This post was last modified: 02-04-2021, 04:03 PM by InsiteFX.)

You do not need __remap

This should do it for you.

PHP Code:
$uri = (string)$this->request->uri;

echo 
$this->uri->getSegments();       // ['path', 'to', 'page']
echo $this->uri->getSegment(1);       // 'path'
echo $this->uri->getTotalSegments();  // 3 

Try that.

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 02-05-2021, 12:36 AM by brianjamestorr.)

If I doesn't use _remap then I need to make url like this mywebsite.com/history/index/12 because it is saying that "page is not found", is there a way to remove that "index" from url somehow ?

Also with those you suggested I am getting errors when page is loaded
Message: Undefined property: History::$request
Message: Trying to get property 'uri' of non-object
Message: Call to undefined method CI_URI::getSegments()

EDIT
I removed _remap and made route like this $route['history/(:any)'] = 'history/index/$1'; this fixed all.
Reply
#4

Anytime that your receiving data into a method you should have a matching route.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB