ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[CJAX] ajax won't work when called from any method other than index() in my application/controllers controller

April 09, 2014 4:06am

Subscribe [2]
  • #1 / Apr 09, 2014 4:06am

    salim.imani

    2 posts

    Hello,

    I’ve been trying to use Cjax Framework while still preserving the codeigniter call structure in my project

    For instance, I want to use /ajax/ajxctrl/ajaxmthd to call controller “ajaxctrl” located in application/response only when i need a json reponse, e.g. for autocomplete purposes, and keep using application/controllers/ when I want to display my views, not having to switch regularly between urls like http://localhost/CIproject/ajax/ctrl/mthd and http://localhost/CIproject/controller/method and having frontend controllers split between two locations.
    For that I’ve used require_once(FCPATH.‘ajaxfw.php’); in my frontend controller

    I am experiencing some troubles, I hope you’ll be able to help me :

    In my project, I have “regular” controllers (to call the view) in the “application/controllers” folder, and the controllers for ajax in “application/response”
    In my controller, I’ve noticed that when I put the actions in the index() function, ajax works and I see the contents (although it behaves strangely, since it doesn’t work all the time and sometimes one or two clicks on “refresh” do the trick). But when I put my actions in another function (eg. “showAutocomplete()”) nothing happens except that I see the input field.

    Basically it looks like this :
    /application/controllers/
    ...........frontend.php
    /application/views/
    ...........myview.php
    /application/response/
    ...........autocompdata.php

    ======== frontend.php ==========

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Frontend extends CI_Controller {
    
     public function showAutocomplete()
     {
      require_once(FCPATH.'ajaxfw.php');
      $ajax = ajax();
      $ajax->keyup('#text1', $ajax->autocomplete('ajax/autocompdata/update'));
      $this->load->view('myview');
     }
    }

    ========  myview.php ==========

    <html>
      <head>
        <?php echo ajax()->init(); ?>
      </head>
      <body>
        <input type="text" id="text1" value="" />
      </body>
    </html>

    =======  autocompdata.php =======

    <?php
    
    class Autocompdata extends CI_Controller {
    
     function __construct()
     {
      parent::__construct();
     }
     
     function update()
     {
      $data = array('test11','test12','test3');
      return $data;
     }
    }

    So I call my view from the frontend controller like this : “http://localhost/myproject/frontend/showAutocomplete”
    It shows me the input field, but nothing happens when I type in it. No ajax works : I’ve tried with success messages, still nothing shows.
    If I rename showAutocomplete() into index() and call it like this : “http://localhost/myproject/frontend/” it works just fine, autocomplete, success message, and all. (Although I sometimes need to navigate to “http://localhost/myproject/ajax/autocompdata/update” and back to “http://localhost/myproject/frontend/” before it works for the first time, like with some sort of off/on switch)

    I know it is quite a strange behaviour, is there something I am doing wrong ? The strangest part is that whether it works or not depends on whether I call my main function “index” or something else.

    Thank you in advance for the help !
    And sorry for the very long post, it was difficult to explain my situation clearly…

    Salim

  • #2 / Jun 10, 2014 6:10am

    Ajaxboy

    243 posts

    make sure strict standards are off in your php.ini

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases