How using Ajax In Opencart?

I minim experience in ajax so i just try my best.

I using Opencart Version

And I try build ajax for result data

Eg (select title,description,date from menu where menu_id ='1') or (select *).

In my case i using combobox for trigger ajax, so I using Onchange in combobox then passing variable to Controller.

Ajax :

<script type="text/javascript" src="view/javascript/jquery/jquery-1.7.1.min.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $('#id_menu').change(function() {
            if ($(this).val != 0) {
                var menu_id = $(this).val();          // store to variable for passing
                url: 'index.php?route=menu/all_menu/function_menu',
                type: 'POST',
                dataType: 'html',
                data: {
                menu_id: menu_id
            success: function(response) {

View :

<?php echo $header; ?>
<div id="content">
    <div class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb){?>
            <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
        <?php } ?>

    <?php if ($error_warning) {?>
        <div class="warning"><?php echo $error_warning; ?></div>
    <?php } ?>

    <?php if ($success) {?>
        <div class="success"><?php echo $success; ?></div>
    <?php } ?>

    <div class="box">
        <div class="heading">
            <h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>

        <div class="content">
            <form action="<?php echo $action; ?>" method="POST" enctype="multipart/form-data" id="form">
                <table class="form">

                        <td><?php echo $entry_menu; ?></td>
                            <select name="id_menu" id="id_menu">
                                <option value="" selected='selected'> Menu </option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>    
                            <?php if ($error['menu'])?>
                                <span class="error"><?php echo $error['menu']; ?></span>

                <div id="all-menu" align="center">


(class ControllerMenuAllMenu extends Controller {)
public function function_menu(){
    echo "A";exit;         //Should be result A

When I change combo box it's display my home page(backend) again in div all-menu

Like This



If you are calling an admin page you need to include your session token in the ajax url:

url: 'index.php?route=menu/all_menu/function_menu&token=<?php echo $this->session->data["token"]; ?>',


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.