3

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

5
  • Seams like you are doing it right. You should get an array in your controller in the var $vat_exempt_name. Just make sure the form action is post.
    – Marius
    Oct 17, 2014 at 7:32
  • @marius I am not using any from Oct 17, 2014 at 7:33
  • 3
    Then that's the problem. How do you expect to get your values to a controller if you don't send them via post or get? Maybe you should provide more code in your question and an explanation of what are you trying to achieve.
    – Marius
    Oct 17, 2014 at 7:35
  • 1
    Pratik, I don't want to be offensive, bout it looks like you lack the basis of HTTP and PHP: php.net/manual/en/tutorial.forms.php Oct 17, 2014 at 7:55
  • @FabianBlechschmidt Thanks for your guidance but I was doing something with ajax as I didnt wanted to reload the page Oct 17, 2014 at 8:46

1 Answer 1

1

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;

5
  • Can you extend this answer with a prototype solution as well? (as Magento doesn't use jQuery by default) Oct 17, 2014 at 10:58
  • @JonathanHussey i have edited my code without using jquery. Oct 17, 2014 at 11:09
  • @JonathanHussey actually since 1.9 it does in RWD theme. Dec 14, 2014 at 5:54
  • Yes it does use jQuery from 1.9, but prototype is still the core framework. jQuery is just used for the RWD theme and prototype should really still used in preference in Magento 1. Dec 15, 2014 at 8:47
  • im facing same issue here: can u please suggest magento.stackexchange.com/questions/144361/…
    – Sushivam
    Nov 6, 2016 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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