How using Ajax In Opencart?

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

I using Opencart Version 1.5.5.1

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
                $.ajax({
                url: 'index.php?route=menu/all_menu/function_menu',
                type: 'POST',
                dataType: 'html',
                data: {
                menu_id: menu_id
            },
            success: function(response) {
                $("#all-menu").html(response);
            }
          });
        }
      }).change();
    });
</script>

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 } ?>
    </div>

    <?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>

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

                    <tr>
                        <td><?php echo $entry_menu; ?></td>
                        <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>    
                            </select>
                            <?php if ($error['menu'])?>
                                <span class="error"><?php echo $error['menu']; ?></span>
                        </td>           
                    </tr>

                </table>
                <div id="all-menu" align="center">
                </div>
            </form>
        </div>
    </div>
</div>

Controller:

(class ControllerMenuAllMenu extends Controller {)
public function function_menu(){
    $this->language->load('menu/all_menu'); 
    $this->load->model('menu/all_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 http://s1064.photobucket.com/user/blackarch01/media/Untitled_zpsbkmv1vlk.jpg.html?sort=3&o=0

Answers:

Answer

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"]; ?>',

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.