Magento – pass data from phtml file to controller

ajaxcontrollersjavascripttemplate

I want to pass data from my 'phtml' template file to controller file please help me to do this.

this is my code for text box in phtml file
<input type="text" class="input-text watch-keyup" id="vat_exempt_name[<?php echo $pid ?>]" name="vat_exempt_name[<?php echo $pid ?>]" value="<?php echo $vat_exempt_name; ?>">

I am calling it as

$vat_exempt_name = $this->getRequest()->getPost('vat_exempt_name');
in my controller file .Please help me if I am wrong anywhere

Best Answer

if you are doing it with ajax,then Try this,

var name = jQuery(".input_text").val();//make sure you have used 'input_text'for this input element only
jQuery.ajax({
            type:"POST",
            data:{vat_exempt_name:name},//see ajax syntax for remaining

The above code can help you to pass params as post to any controller.

or

instead using jquery you can getting value of an element with simple javascript as, var name = document.getElementById('input_field_id').value;

Related Topic