3

I trying to load a view into my div #teste with this.id through JavaScript without success. What's wrong on my function?

JavaScript function below

<script type="text/javascript">

var baseurl = '<?php site_url(); ?>'; //raiz do website

function navbutton(id) {

    $.ajax({
        type: 'GET',
        url: baseurl+'main/'+id,
        dataType: 'html',
        success: function(html) {
            $('#teste').html(html);
        }
    });
    }

HTML button

<li><a href="javascript:void(0)" id="contato" onclick="navbutton(this.id)">Contato</a></li>

My main Controller (one function to call all others views)

class Main extends CI_Controller {

var $_data = array();

public function index()
{
    if (!$this->uri->segment(1) && !$this->uri->segment(2)) {
        $view = 'inicio';
    }
    if ($this->uri->segment(1) && !$this->uri->segment(2)) {
        $view = $this->uri->segment(1, 'inicio');
    }
    if ($this->uri->segment(1) && $this->uri->segment(2)) {
        $view = $this->uri->segment(1, 'inicio') . '/' . $this->uri->segment(2, '');
    }
    if (!file_exists(APPPATH . 'views/' . $view . EXT)) {
        $view = "404";
        header("HTTP/1.0 404 Not Found");
    }

    $this->load->view('templates/header');
    $this->load->view($view, $this->_data);
    $this->load->view('templates/footer');

}
5
  • could you explain where you have errors? or what actually happen? Please get more details to give you needed answer Commented May 2, 2015 at 23:16
  • are the ajax base url same with browser ? because javascript has "Same origin policy" Commented May 2, 2015 at 23:45
  • i sugest to add error:function(v1,v2,v3){ alert('Something went wrong'); } on ajax , after sucess: { ....} to catch some response errors Commented May 2, 2015 at 23:47
  • i always get 'Uncaught ReferenceError: navbutton is not defined' even with $(document).ready(function() Commented May 2, 2015 at 23:57
  • take a look on my last edit of answer. If you have already solved, please add here your solution. Commented May 3, 2015 at 11:30

2 Answers 2

1

to avoid to get 'Uncaught ReferenceError: navbutton is not defined' error attach on click event function with javascript:

var baseurl = '<?php site_url(); ?>'; //raiz do website
function navbutton(id) {
    $.ajax({
        type: 'GET',
        url: baseurl+'main/'+id,
        dataType: 'html',
        success: function(html) {
            $('#teste').html(html);
        },
        error:function(v1,v2,v3){
            alert('Something went wrong'); }
    });
}

$("#contato").on('click',function(event){
    event.preventDefault();
    navbutton(this.id);
});

And on HTML use

<li><a href="#" id="contato">Contato</a></li>

JSFIDLE test - in this example the ajax request will get error 404 because the url is not defined proper but in you code must work.

HINT to make sure all code is in the proper place, take a look in the source code of page after is loaded in browser (CTRL+U)

Sign up to request clarification or add additional context in comments.

3 Comments

on my console still report an error.Uncaught ReferenceError: navbutton is not defined
add the declaration of navbutton before to use-it in html onclick
the example works fine, even with codeigniter -function is declared before my button-, but correction doesn't work with full code. i think is something wrong with ajax instructions, but idk what.
0

Are you trying to load string to element? In that case you need to pass third argument TRUE when loading view method.

$view = $this->load->view('templates/header', '', TRUE);
$view .= $this->load->view($view, $this->_data, TRUE);
$view .= $this->load->view('templates/footer', '', TRUE);
echo $view;

Check in docs here.

2 Comments

I trying to render/load partial view. Load a page with javascript without refresh on codeigniter.
I've just pointed at how you can make a string not making page load. If you use ajax to get that string, although you need to post identifier for specific page, it could work.

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.