title: CakePHP controller to build and send Form as HTML to Jquery UI Dialog Box
body: <p>I am using cakePHP and Jquery UI for a small project.</p>
  
  <p>I have an 'Add Item' link on my index.ctp of Item.</p>
  
  <p>I want to show a jQuery UI dialog with the forum in for adding an Item when the 'Add Item' link is clicked.</p>
  
  <p>So that I can make use of cakePHP's awesome <strong>FormHelper</strong> I want the controller's add() method to return the html the <strong>FormHelper</strong> creates <code>if($this-&gt;request-&gt;is('ajax'))</code> but this is giving me Internal Server Errors (when I Inspect Element in Chrome).</p>
  
  <p>I know this isn't very MVC but my reasoning is that I want my app to work if the user has Js enabled or not, therefore the 'Add Item' link shows the add.ctp View if they don't have jQuery but if they do I've got <code>preventDefault()</code> in my jQuery script.</p>
  
  <p>The jQuery click handler then sets up the dialog box's properties (jQueryUI Dialog Box) and then makes an ajax request to add() for the form.</p>
  
  <p>I copied and pasted the <strong>FormHelper</strong> code I used on add.ctp but instead of echoing, added each line to $htmlString. I then called <code>echo $htmlString; exit();</code> but here I get the <strong>500 Internal Server Error</strong>.</p>
  
  <p>If I comment out all the <strong>FormHelper</strong> lines in the add() function everything works ($htmlString is just "") so there is something going wrong here.</p>
  
  <p>As I said I know this isn't very MVC-y but I can't think of another way to do it. Could I potentially get add.ctp returned rather than the copy and pasted code? That would keep the MVC model right and also if $this->layout = 'ajax' then I will only get the exact add.ctp file code right?</p>
  
  <p>Thanks for your time and help in advance.</p>
  
  <p><strong>Edit - Added jQuery ajax request and cakephp Controller add() function</strong></p>
  
  <p>Jquery ajax request:</p>
  
  <pre><code>$('.add_project').click(function(event) {
          //Kill default
          event.preventDefault();
  
          $.ajax({
              url : $(this).attr('href'),
              type : 'post',
              data : 'ajax=1',
              dataType: 'html',
              success: function(response) {
                  alert(response);
  
                  //Check jquery loaded
                  if(jQuery.ui) {
                      //Fill dialog with form
                      $("#dialog").html(response).show();
  
                      //Show dialog
                      $("#dialog").dialog("open");
                  }
              }
          });
      });
  </code></pre>
  
  <p>CakePHP Controller code:</p>
  
  <pre><code>public function add()
  {
      //If this has been requested with ajax then give the form that we create on add.ctp
      if($this-&gt;request-&gt;is('ajax'))
      {
          App::import('Helper', 'Form');
  
          $this-&gt;loadModel('Project');
          $html = '';
  
          $html += $this-&gt;Form-&gt;create('Project');
           $html += $this-&gt;Form-&gt;input('name');
           $html += $this-&gt;Form-&gt;input('colour', array(
               'options' =&gt; array('blue' =&gt; 'Blue', 'green' =&gt; 'Green', 'red' =&gt; 'Red', 'orange' =&gt; 'Orange', 'yellow' =&gt; 'Yellow')
           ));
          $html += $this-&gt;Form-&gt;end('Create Project');
          echo html;
          exit();
      } 
  </code></pre>
  
  <p>I'm pretty positive it's nothing to do with the ajax request it's more of a problem in cakephp that it doesn't like to use the FormHelper in the controller. I know this is bad MVC practice but I can't think of another way to do this dynamically (as in the fact that cakephp builds the html tags based on the Model).</p>
  
  <p>Thanks again.</p>