4
$event = $fieldset->addField('parent_id', 'select', array(
    'label' => Mage::helper('gallery')->__('Parent'),
    'required' => true,
    'name'=>'parent_id',
    'values'=>$ac,
    'onchange'=>'CheckSelectedItem()',
  )); 
  $event->setAfterElementHtml('<script>
       function CheckSelectedItem()
       {
       var ddllist= window.document.getElementById("parent_id");
       var itemName= ddllist.options[ddllist.selectedIndex].value;

how to make an ajax call on form.php for the file that resides in root folder called "gallerydata.php". i have an extension called "gallery" for uploading image from backend. so i want to get an id of artist from dropdown by using ajax which makes call to that file "gallerydata.php".

       if(window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          xmlhttp1=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {

             alert(xmlhttp.responseText);

            }
          }

        xmlhttp.open("GET","http://122.170.97.189:81/electriccityusa/gallerydata.php?q="+itemName,true);

       }

    </script>');     
Sylvain Rayé
  • 2,456
  • 16
  • 23
Hardik Patel
  • 41
  • 1
  • 2

1 Answers1

11

You can simply use ajax in adminhtml form as:

$event = $fieldset->addField('parent_id', 'select', array(
    'label'     => Mage::helper('gallery')->__('Parent'),
    'required'  => true,
    'name'      => 'parent_id',
    'values'    => $ac,
    'onchange'  => 'checkSelectedItem(this)',
  )); 
$event->setAfterElementHtml("<script type=\"text/javascript\">
    function checkSelectedItem(selectElement){
        var reloadurl = '". $this->getUrl('your-module-controller-action')."parent_id/' + selectElement.value;
        new Ajax.Request(reloadurl, {
            method: 'get',
            onLoading: function (transport) {
                $('parent_id').update('Searching...');
            },
            onComplete: function(transport) {
                    $('parent_id').update(transport.responseText);
            }
        });
    }
</script>");

Now you can fetch the select value and do operation as per required in your custom module controller action (mentioned in reloadurl).

Hope this helps.

MagePsycho
  • 1,944
  • 2
  • 29
  • 60
  • What do I use for your-module-controller-action? I'm trying to trigger a reload of the category page by calling catalog_category/edit, but the ajax keeps going to the wrong url (catalog_category/index instead of /edit) – Benubird Mar 25 '13 at 09:56
  • @MagePsycho how can i append or set ajax return value, I am unable to set this – Pushpendra Singh Jul 23 '15 at 06:59