0

Hello I have made a custom admin module, in which I am uploading image using using Ajax and for this I am using ajaxfileupload library.

this is my edit.php file code which uses ajax code-

<?php
class Assel_Character_Block_Adminhtml_Character_Edit extends Mage_Adminhtml_Block_Widget_Form_Container {

    public function __construct() 
    {
        parent::__construct();                      
        $this->_objectId = 'id';
        $this->_blockGroup = 'character';
        $this->_controller = 'adminhtml_character';
        $this->_mode='edit';


        $this->_updateButton('save', 'label', Mage::helper('character')->__('Save Character'));
        $this->_updateButton('delete', 'label', Mage::helper('character')->__('Delete Character'));


        $this->_addButton('saveandcontinue', array(
            'label'     => Mage::helper('adminhtml')->__('Save And Continue Edit'),
            'onclick'   => 'saveAndContinueEdit()',
            'class'     => 'save',
        ), -100);

        $pathToMethod=$this->getUrl('*/*/fileUpload'); /*url of method in controller file to process on file or image*/

        $this->_formScripts[] = "var pathToMethod="."'".$pathToMethod."'";/*assign url of controller method in js variable to use in ajax below*/
        $script  = 
<<<EOD

            function saveAndContinueEdit(){
                editForm.submit($('edit_form').action+'back/edit/');
            }

            jQuery(function($){
                var btnUpload=$('#upload');
                var status=$('#status');

                new AjaxUpload(btnUpload, {
                    action:'pathToMethod', /*call the method in controller file in custom module*/
                    name: 'uploadfile',
                    onSubmit: function(file, ext){
                         if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 

                            status.text('Only JPG, PNG or GIF files are allowed');
                            return false;
                        }
                        status.text('Uploading...');
                    },
                    onComplete: function(file, response){

                        status.text('');

                        if(response==="success"){
                            $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
                        } else{
                            $('<li></li>').appendTo('#files').text(response).addClass('error');
                        }
                    }
                });

            });
EOD;

        $this->_formScripts[] = str_replace(array("\r\n", "\r", "\n"), "", $script );

    }

My problem is this Ajax call is giving the HTML of whole page of System->configuration in admin in response instead of calling method defined in controller.

What is wrong there in my code, somebody there to help me.?

1

There could be several things wrong here but your action for the Ajax would be a good place to start:

action:'pathToMethod' should be: action:pathToMethod

pathToMethod is a variable and should be used as one and not as a string.

0

Since the pathToMethod variable is created in PHP and you want to use it in the script you should probably echo or concatenate it to your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.