skip to Main Content

I am new in magento,
i have create a phtml template file in magento 1.9.
i that phtml i have create a html form. i want to call a function after submitting that form. i am little confused how i should write a function shall i create a controller or a block or in same phtml i have write a function.
please guide me with correct
`

            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 content-row">
                <h2>PLEASE ENTER</h2>
            </div>

            <div class="col-lg-12">
                <div class="col-sm-4 col-md-4 col-lg-4 col-xs-4">
                    <div class="form-group">
                        <label>name</label><br><br> 
                        <input class="form-control" maxlength="2">
                    </div>
                </div>

`

2

Answers


  1. It is linked with MVC pattern. To make it short, you need to write a new action on the controller.

    For example, if your form is supposed to send an email you can write in the controller that is responsible for the display of your form a new method called sendAction(). In this method, you will make a check of your form and send an email if it must be.

    Blocks hold logics of the block itself (retrieve data that will be displayed for example)

    Phtml files are only for the display and even though you can see in Magento files phtml with logic its a bad habit and has historical reasons.

    To finish, if the logic of your form is held by the Magento controller you need to override the controller and neither touch the Core.

    Login or Signup to reply.
  2. there are few ways you an do this: https://www.w3schools.com/jsref/event_onsubmit.asp

    Here is an simple example how you can run your JS on form submit.

    <form name="yourform" onsubmit="your_script()" >
        <input type="text" name="text"/>
      <input type="submit" name="Submit" value="Submit"/>
    </form>
    
    function your_script() {
    //your script here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search