Question

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(){}
Was it helpful?

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>

OTHER TIPS

You need to be more specific on why you want to call your actions like that.

You can use AJAX to fetch some data produced by your actions or you can do a simple redirect. It depends on what you want to achieve.

The AJAX solution:

  1. attach an onchange event handler to select
  2. the handler issues an AJAX request to the server, to the correct action. You can easily output JSON data from the controller using $this->_helper->json($data)
  3. you get back information from server and inject to your HTML document

change your html to this

<select id="your_id">
  <option value="Threes">3*3</option>
  <option value="Six">6*6</option>
  <option value="Twele">12*12</option>
</select>

in js write this

$("#your_id").change(function() {
var action = $("#your_id").val();
 var href= "Controller/"+action +"/";
     var data = your data;

     $.ajax({ type: "GET",
           url: href,
          data: data,
          success: function(response){

                  //do what u wana do
         }
           });
   }):

hope it works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top