Search code examples
zend-frameworkzend-form-elementzend-controller

How to Call Controller Action Through html Select options on change in Zend Framework?


i have the following html in my view

<span style="float:right">  <label> Pattern :</label>
   <select>
     <option value="3*3">3*3</option>
     <option value="6*6">6*6</option>
     <option value="12*12">12*12</option>
  </select>
</span>

what i want to do is that i want to call my action onchange lets say

   if(3*3) call this
    public function 3by3Action(){}

   if(6*6) call this
    public function 6by6Action(){}

   if(12*12) call this
    public function 12by12Action(){}

Solution

  • In Zend Framework, Actions are methods within a Controller class and are accessible via URL. For example, if your Controller class is called "MathController" and it contains an action called "sixBySixAction", then you would trigger this action by navigating to a URL that looks something like:

    http://baseUrl/math/six-by-six
    

    Notice that the name of the action method is camel case within the controller class but it is separated by dashes in the URL. This is a formatting requirement for Zend Framework. Also note that the controller class is named "MathController" but you only have to put "math" in the URL.

    So, you could use JavaScript to assign an onChange handler for your select box which simply redirects to a particular URL which handles the change by accessing a particular action method within a particular controller class.

    For more information on this, check out this page in the Zend Framework Programmer's Reference Guide.

    As for the JavaScript part, here is an example of how to redirect upon a select box being changed. You'll need to modify this, of course, but it'll get you started:

    <Script language="JavaScript">
      function goto(form) { var index=form.select.selectedIndex
        if (form.select.options[index].value != "0") {
          location=form.select.options[index].value;}
        }
    </SCRIPT>
    
    <FORM NAME="form1">
      <SELECT NAME="select" ONCHANGE="goto(this.form)">
        <OPTION VALUE="">-------Choose a Selection-------</OPTION>
        <OPTION VALUE="index.htm">Home</OPTION>
        <OPTION VALUE="web_development.htm">Web Development</OPTION>
        <OPTION VALUE="html_codes.htm">HTML Tips</OPTION>
        <OPTION VALUE="html_codes_chart.htm">HTML Code Chart</OPTION>
        <OPTION VALUE="javascript_codes.htm">JavaScript Codes</OPTION>
        <OPTION VALUE="216_color_chart.htm">Color Code Chart</OPTION>
      </SELECT>
    </FORM>