title: Posting Form Data to ZF2 Controller With Ajax
body: <p><strong>EDIT-
  <br>
  I have posted the answer below.</strong></p>
  
  <p>The question is that I don't understand how/where ZF2 posts form data when a submit button is pressed. So, when I do <br> <code>if ($this-&gt;getRequest()-&gt;isPost()){</code>
  <br> after the ajax call below, it tells me that no data has been posted.
  <br>
  <br>When I do the above <code>isPost()</code> if statement it works perfectly when I hit the submit button, telling me the data has been posted and subsequently telling me that the form data 
  is valid.</p>
  
  <p>Here is the ajax call-</p>
  
  <pre><code>            &lt;script&gt;
                  $.ajax({
                      url: urlform,
                      type: 'POST',
                      dataType: 'json',
                      contentType: "application/json; charset=utf-8",
                      async: true,
                      data: ($("#newThoughtForm").serialize() + '&amp;submit=go'),
                      success: function () {
                          console.log('SUBMIT WORKS');
                          setTimeout(function () { &lt;?php echo $this-&gt;invokeIndexAction()-&gt;test(); ?&gt; ;
                          }, 1000);
                      },
  //This keeps getting executed because there is no response, as the controller action is not run on a Post()
                      error: function () {
                          console.log('There is error while submit');
                          setTimeout(function () { &lt;?php echo $this-&gt;invokeIndexAction()-&gt;test(); ?&gt; ;
                          }, 1000);
                      }
  //I assume the data won't get pushed to the server if there is no response,
  //but I can't figure out how to give a response in ZF2 since the controller is not
  //run when the Post() is made. 
                  });
  </code></pre>
  
  <p>Here's the Form-
                  
  
  <pre><code>            use Zend\Form\Form;
  
              class newAlbumForm extends Form
              {
                  public function __construct()
                  {
                      parent::__construct('newAlbumForm');
                      $this-&gt;setAttribute('method', 'post');
  
                      $this-&gt;add(array(
                          'type' =&gt; 'AlbumModule\Form\newAlbumFieldset',
                          'options' =&gt; array(
                              'use_as_base_fieldset' =&gt; true
                          )
                      ));
  
                      $this-&gt;add(array(
                              'name' =&gt; 'submit',
                                  'attributes' =&gt; array(
                                      'type' =&gt; 'submit',
                                      'value' =&gt; 'go'
                          ),
                      ));
                  }
              }
  </code></pre>
  
  <p><br>
  <br>
  The request for the ajax call-</p>
  
  <pre><code>            Request URL:http://test/newAlbum.html
              Request Method:POST
              Status Code:200 OK
              Request Headersview source
              Accept:*/*
              Accept-Encoding:gzip,deflate,sdch
              Accept-Language:en-US,en;q=0.8
              Connection:keep-alive
              Content-Length:46
              Content-Type:application/x-www-form-urlencoded; charset=UTF-8
              Cookie:PHPSESSID=h46r1fmj35d1vu11nua3r49he4
              Host:test
              Origin:http://test
              Referer:http://test/newAlbum.html
              User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
              X-Requested-With:XMLHttpRequest
              Form Dataview sourceview URL encoded
              album[albumText]:hello world
              submit:go
              Response Headersview source
              Connection:Keep-Alive
              Content-Length:4139
              Content-Type:text/html
              Date:Sun, 20 Oct 2013 16:52:15 GMT
              Keep-Alive:timeout=5, max=99
              Server:Apache/2.4.4 (Win64) PHP/5.4.12
              X-Powered-By:PHP/5.4.12
  </code></pre>
  
  <p>The request for the submit button-</p>
  
  <pre><code>            Request URL:http://test/newAlbum.html
              Request Method:POST
              Status Code:200 OK
              Request Headersview source
              Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
              Accept-Encoding:gzip,deflate,sdch
              Accept-Language:en-US,en;q=0.8
              Connection:keep-alive
              Content-Length:46
              Content-Type:application/x-www-form-urlencoded
              Cookie:PHPSESSID=h46r1fmj35d1vu11nua3r49he4
              Host:test
              Origin:http://test
              Referer:http://test/newAlbum.html
              User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
              Form Dataview sourceview URL encoded
              album[albumText]:hello world
              submit:go
              Response Headersview source
              Connection:Keep-Alive
              Content-Length:4139
              Content-Type:text/html
              Date:Sun, 20 Oct 2013 16:52:14 GMT
              Keep-Alive:timeout=5, max=100
              Server:Apache/2.4.4 (Win64) PHP/5.4.12
              X-Powered-By:PHP/5.4.12
  </code></pre>
  
  <p><br>
  <br>
  Here is the indexAction() on the controller for completeness-</p>
  
  <pre><code>            public function indexAction()
              {
                  echo 'console.log("Index Action is Called");';
  
                  $form = new \AlbumModule\Form\newAlbumForm();
                  if ($this-&gt;getRequest()-&gt;isPost()){
                      echo 'console.log("Data posted");';
  
                      $form-&gt;setData($this-&gt;getRequest()-&gt;getPost());
                      if ($form-&gt;isValid()){
                          echo 'console.log("Form Valid");';
  
                          //todo
                          $this-&gt;forward()-&gt;dispatch('newAlbum', array('action' =&gt; 'submitAlbum'));
                          return new ViewModel(
                              array(
                                      'form' =&gt; $form
                              )
                          );
                      } else {
                          echo 'console.log("Form Invalid");';
  
                          return new ViewModel(
                              array(
                                      'form' =&gt; $form
                              )
                          );
                      }
                  } else {
                      echo 'console.log("No data posted")';
                      return new ViewModel(
                              array(
                                      'form' =&gt; $form
                              )
                          );
                  }
              }
  </code></pre>
  
  <p>As I stated at the beginning, the <code>isPost()</code> class will return a value of true when the button submits the form, but it will return a value of false when the form is submitted through Ajax.</p>
  
  <p><strong>EDIT-
  <br>
  I have posted the answer below.</strong></p>