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

  • SEARCH
  • Home
  • 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 7040097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:52:39+00:00 2026-05-28T01:52:39+00:00

Im opening a form via ajax (using jquery) and i want to save it

  • 0

Im opening a form via ajax (using jquery) and i want to save it without reloading whole page. Where i have to change code, to get text response (eg. “ok!”) instead of redirection to edit page?

this is my js:

$("#newcatlink").click(function() {
    $.ajax({
    'url': "category/new",
    'success': function(data, textStatus, jqXHR) {
            $("#mdialog").html(data);
            $("#_form_jqhide").hide();
            $("#mdialog").dialog({
                modal: true,
                width: 500,
                resizable: false,
                buttons: {
                    Save: function() {
                        $.ajax({
                            'type': 'POST', 
                            'url': 'category/create', 
                            'data': $("#categoryform").serialize(),
                            'success': function(data, textStatus, jqXHR) {
                                alert(data);
                            }
                        });
                    },
                    Cancel: function() {
                        $(this).dialog("close");
                    }
                }
            });
        }
    });
});

I changed processForm action in actions.class.php like this:

...
if ($form->isValid())
{
  $Category = $form->save();

        if($request->isXmlHttpRequest()) {
            return $this->renderText("ok!");
        } else {
            $this->redirect('category/edit?id='.$Category->getId());
        }
}

but it didnt change anything. How to do this?

edit 8/1/12:

I’ve made changes to my code according to blowski and sakfa answers, but i get another issue: how to catch form errors? I added to processForm else to if and it works as expected but i cant get error message/code/whatever back, only a fact that error exists.

Here is code: http://pastebin.com/ppbPM88G
and this is server answer after sending form with one required field empty:

{“haserrors”:true,”errors”:[]}

  • 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-05-28T01:52:40+00:00Added an answer on May 28, 2026 at 1:52 am

    Based on our conversation, I can see what’s happening. If I’ve understood your comments, it’s not redirecting the whole page, it’s just re-rendering the original form inside the dialog box?

    After the user submits the form, if it’s valid, then it redirects. However, because you’re bypassing the redirect, it reaches the $this->setTemplate('new') bit of code instead, so shows the new form again, populated with the data from the just-completed form.

    To get around it, use something like this in the create action:

    if($request->isXmlHttpRequest()) {
      $this->renderText(json_encode(array('valid' => $form->isValid())));
    } else {
      $this->setTemplate('new');
    }
    

    You’ll also want to change your Ajax request to specify that it’s expecting a JSON response. Then, if the form was valid, close the dialog box (or whatever you want to do after the save).

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

Sidebar

Related Questions

While I was trying to refresh page contents dynamically using Ajax/JQuery, I have learned
I have an HTML form on my site that submits via Ajax to a
Using colorbox.js, I am opening a form in a lightbox when the user clicks
I have a HTML form that has certain fields which i am opening inside
i am opening jQuery Dialog through the below code.The problem here is when i
I have a dashboard page where the user can enter an asset code or
I have a CustomInstaller class (System.Configuration.Install.Installer) and basically I'm opening a dialog form at
Hi i have this form opening with window's dialog: <form method=GET id=formparam> <table><tbody><tr><td> <label>ID</label></td>
I have a web form where users answer questions via dropdowns, and I wish
I am using the below function to Close existing form and opening new form.

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.