1

I used ajax to get data, everything is fine, but I want to assign data to a php variable and foreach it to create an html select, how do I do it? please help me

My ajax

var contactUrl = '<?php echo $this->getUrl('*/*/getContact')?>';
jQuery.ajax({
    url: contactUrl,
    data: {idAddress: addrId},
    success: function(data) {
         console.log(data);
    }
});

My data

ar

  • You need to create html select by JavaScript or JQuery after getting data successfully from ajax. and then put created html anywhere in your page as per your requirement – Naresh Rupareliya Nov 13 at 10:11
0

Instead of creating a separate request you can assign values to the PHP variable in that Ajax controller itself.

If you want to access your variables across all the class you can assign that in session.

0

OPTION 1

I would recommend moving the function to a Helper file and using PHP to call it.

So move getContactAction() to your modules Helper file Data.php and reference it like so in your template file.

// Replacing namespace and modulename with your company name and module name
$data = Mage::helper('namespace/modulename')->getContact($idAddress);

In your Helper redefine function like so:

public function getContact($idAddress) {
    // Remove any references to getParams (no longer an AJAX request)
    ...
}

OPTION 2

Instead of using PHP foreach to render the select, use JS

<input id="myinputfield"/>

var contactUrl = '<?php echo $this->getUrl('*/*/getContact')?>';
jQuery.ajax({
    url: contactUrl,
    data: {idAddress: addrId},
    success: function(data) {
         console.log(data);
         var inputField = jQuery("#myinputfield");
         jQuery.each(data, function(index, value) {
             jQuery('#mySelect').append(jQuery('<option>', { 
                 value: value.salesforceintegrationsuite_contact_id,
                 text : value.account_id
             }));
         }
    }
});

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.