Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:33:56+00:00 2026-06-15T02:33:56+00:00

I am using cakePHP and Jquery UI for a small project. I have an

  • 0

I am using cakePHP and Jquery UI for a small project.

I have an ‘Add Item’ link on my index.ctp of Item.

I want to show a jQuery UI dialog with the forum in for adding an Item when the ‘Add Item’ link is clicked.

So that I can make use of cakePHP’s awesome FormHelper I want the controller’s add() method to return the html the FormHelper creates if($this->request->is('ajax')) but this is giving me Internal Server Errors (when I Inspect Element in Chrome).

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 preventDefault() in my jQuery script.

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.

I copied and pasted the FormHelper code I used on add.ctp but instead of echoing, added each line to $htmlString. I then called echo $htmlString; exit(); but here I get the 500 Internal Server Error.

If I comment out all the FormHelper lines in the add() function everything works ($htmlString is just “”) so there is something going wrong here.

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?

Thanks for your time and help in advance.

Edit – Added jQuery ajax request and cakephp Controller add() function

Jquery ajax request:

$('.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");
                }
            }
        });
    });

CakePHP Controller code:

public function add()
{
    //If this has been requested with ajax then give the form that we create on add.ctp
    if($this->request->is('ajax'))
    {
        App::import('Helper', 'Form');

        $this->loadModel('Project');
        $html = '';

        $html += $this->Form->create('Project');
         $html += $this->Form->input('name');
         $html += $this->Form->input('colour', array(
             'options' => array('blue' => 'Blue', 'green' => 'Green', 'red' => 'Red', 'orange' => 'Orange', 'yellow' => 'Yellow')
         ));
        $html += $this->Form->end('Create Project');
        echo html;
        exit();
    } 

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).

Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:33:57+00:00Added an answer on June 15, 2026 at 2:33 am

    The 500 Internal Server error you are receiving is because you are using a Helper within a Controller.

    However! you can pass a view back through your ajax request.
    Create the method in your controller. Create the view with the form helper

    ajaxForm.ctp

    $this->Form->create('Project');
    $this->Form->input('name');
    $this->Form->input('colour', array(
           'options' => array('blue' => 'Blue', 'green' => 'Green', 'red' => 'Red', 'orange' => 'Orange', 'yellow' => 'Yellow')
    ));
    $this->Form->end('Create Project');
    

    controller

    public function ajaxForm(){
    
    if($this->request->is('ajax')){
        $this->layout = 'ajax';   // to avoid returning your full layout
        $this->render('ajaxForm');
    
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using cakephp 2.1 just fine; I want to use jquery to have my
I am using cakePHP for my project and i have the following Jquery script
I am using JQuery and cakephp for my application. I have created a select
Im using cakephp 2.0 and i have data submitted that i want to cleanup,
I'm using cakephp to build a survey and want to use javascript (specifically jquery
  I am developing a project which is built in CakePHP and using jQuery in
I am using jquery tabs within my CakePHP project. Here is my Script: $(function()
I am using cakephp and want to use the jQuery.ajax to post to my
Im using cakePHP 2.2.1 I have a GoalsController that has an add action in
I've set up a test so I can begin using jQuery in a cakePHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.