0

I have a jQuery load() function that calls for a controller function, but instead loads the load->view() from the controller index function resulting into the whole page being loaded inside the div.

HTML "mypage.php"

 <h1 class="output"></h1>
 <button class="button" onclick="accept(); return false;"> Accept</button>

JS:

function accept(){

     $('.output').load('mycontroller/accept');

}

PHP Controller:

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

class Mycontroller extends CI_Controller {



  function index() {

         $this->home();
  } 


  function home() {

         $this->load->view('user/home');
  } 


 /// load page where button is
  function mypage(){

          $this->load->view("user/mypage");



}

 function accept(){

         echo 'HELLO PHP controller';

     }


}

If I remove the index function from the controller, it doesn't happen, but still the "accept" function from the controller doesn't run :(

I have a similar page "home" which is called in the index function. Thus http://localhost:8888/mysite/mycontroller/ it has the same type of load() and it works. However, if I append /index or home after '/mycontroller', I have same problem as the other page "mypage". So is like the load() function only seem to work when pointed to the controller itself without any functions. Would that have anything to do with the .htaccess settings??

HTACCESS:

Options -Indexes

Options +FollowSymLinks

DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index.php
    RewriteRule (.*)\.php$ index.php/$1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

Will appreciate any help. Thanks in advance.

4
  • the rest of your code might help - PHP at least Commented Dec 13, 2014 at 1:46
  • @Deryck just edited and added the missing function that loads the view where the button is. Just like that and still the index is being loaded istead of the accept. This is very strange and I really can't understand why is not working. everything seems right to me. Commented Dec 13, 2014 at 2:00
  • user/home has a similar script and works just fine, but i noticed something: The url is: http://localhost:8888/mysite/mycontroller/ it works fine. But if I add the /index after the controller, the page loads fine but the Jquery load() function does the same as my initial problem on the mypage. So I guess it's a Redirect problem with the routing, could it be the ..htaccess? Commented Dec 13, 2014 at 19:29
  • Show us routes.php. I crafted partial answer for you, will edit when you edit (with routes), please feel free to try out answer as is. Commented Dec 13, 2014 at 22:35

1 Answer 1

1

Please use .htaccess that is tailored for CodeIgniter, rewritebase is important I guess you need to use RewriteBase /mysite. I got it from net a year/two ago don't remember the source (will search for it), source, another one is on gist.

<IfModule mod_rewrite.c>
# mod_rewrite rules
RewriteEngine on

# The RewriteBase of the system (if you are using this sytem in a sub-folder).
 RewriteBase /

# This will make the site only accessible without the "www."
# (which will keep the subdomain-sensive config file happy)
# If you want the site to be accessed WITH the "www."
# comment-out the following two lines.
#RewriteCond %{HTTP_HOST} !^localhost$ [NC]
#RewriteRule ^(.*)$ http://localhost/$1 [L,R=301]

# If a controler can't be found - then issue a 404 error from PHP
# Error messages (via the "error" plugin)
# ErrorDocument 403 /index.php/403/
# ErrorDocument 404 /index.php/404/
# ErrorDocument 500 /index.php/500/

# Deny any people (or bots) from the following sites: (to stop spam comments)
# RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
# RewriteCond %{HTTP_REFERER} porn\.com
# RewriteRule .* - [F]
# Note: if you are having trouble from a certain URL just
# add it above to forbiden all visitors from that site.

# You can also uncomment this if you know the IP:
# Deny from 192.168.1.1

# If the file is NOT the index.php file
RewriteCond %{REQUEST_FILENAME} !index.php
# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)\.php$ index.php/$1

# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

</IfModule>

# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>

#<ifModule mod_expires.c>
#  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|js|css|swf)$">
#       ExpiresActive on
#       ExpiresDefault "access plus 1 year"
#   </filesmatch>
#</ifModule>

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/xhtml text/html text/plain text/xml
text/javascript application/x-javascript text/css
</IfModule>

JavaScript

Define base_url for JavaScript scope, so you can use base_url in your JavaScript files.

Either make a helper, or assign base_url in every page on every load on top of the page.

<script>
    var base_url = <?= base_url()?>
</script>

Put code above in some kind of view that is loaded always (before any other JavaScript code is executed).

Change your JavaScript code to this one

function accept() {
    $('.output').load(base_url + '/mycontroller/accept'); //fix leading slash by inspecting
}
Sign up to request clarification or add additional context in comments.

1 Comment

passing the base_url to javascript did the trick ;) thanks so much. Although the htaccess you posted is giving me an internal server error. I'm still using the same I was using. Unless I might get in trouble in the future.

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.