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 8800977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:43:38+00:00 2026-06-14T00:43:38+00:00

I have a form in Symfony2 project and I need to submit form. The

  • 0

I have a form in Symfony2 project and I need to submit form. The first idea was to submit using ajax but the problem is that this will not pass the symfony2 required validation and I am getting the CSRF error message (see my precedant question : Symfony2: The CSRF token is invalid. Please try to resubmit the form).

Thank’s to @Elnur’s answer, I am able now to submit the form using $post but have another problem.

With Ajay, I am getting back an html response and I was able to asign this response to an HTML element:

$.ajax({
    type: "POST",
    async: true,
    url: href,
    cache: false,
    dataType: "json",
    success: function(data) {
        $("#content .contentwrap .itemwrap").html( data.content );
    }
});

Here is the response I am getting from:

<div class="item item-last">
<h1>Create Affiliation</h1>
        <div class="error">
        <ul><li>The CSRF token is invalid. Please try to resubmit the form</li></ul>
        <ul><li>The Affiliation should not be blank</li></ul>

    </div>

</div>
<div class="item item-last">
<form action="/app_dev.php/user/submission/affiliation/create/4a0ad9f8020c5cd5712ff4c4c8921b32?ajax=no" method="POST" class="authorForm" >
    <div style="float:left;">
        <table width="100%" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <label for="submissionAffiliationForm_affiliation" class=" required">Affiliation</label>
                </td>
                <td>
                    <input type="text" id="submissionAffiliationForm_affiliation" name="submissionAffiliationForm[affiliation]" required="required"    size="40" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <div class="button button-left button-cancel">
                        <img src="/bundles/sciforumversion2/images/design/new/button-red.png"/>
                        <a href="/app_dev.php/user/submission/author/edit/4a0ad9f8020c5cd5712ff4c4c8921b32/0" class="submission_link">cancel</a>
                    </div>
                    <div style="float: left;">&nbsp;</div>
                    <div class="button button-left button-cancel">
                        <img src="/bundles/sciforumversion2/images/design/new/button.png"/>
                        <input type="submit" name="login" value="submit" />
                    </div>
                    <div style="clear: both;"></div>
                </td>
            </tr>
        </table>
    </div>

    <input type="hidden" id="submissionAffiliationForm__token" name="submissionAffiliationForm[_token]" value="de9690f61f0ee5f30fdcc5152f44e76787f34bbb" />

</form>
</div>

But now, when using post:

$.post($this.attr('action'), $this.serialize(), function (data) {
    $("#content .contentwrap .itemwrap").html( data );
});

I am not getting anymore HTML but a JSON formated response and I don’t know how to extract he right information from.

Here is the response I am getting from the post:

{"responseCode":400,"errors":false,"submitted":false,"content":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\n        <meta http-equiv=\"refresh\" content=\"1;url=\/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32\" \/>\n\n        <title>Redirecting to \/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32<\/title>\n    <\/head>\n    <body>\n        Redirecting to <a href=\"\/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32\">\/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32<\/a>.\n    <\/body>\n<\/html>","notice":""}

Here is how it looks like th controller:

$em         = $this->getDoctrine()->getEntityManager();
    // get the user object
    $user       = $this->get('security.context')->getToken()->getUser();
    $submission = $em->getRepository('SciForumVersion2Bundle:Submission')->findOneBy( array("hash_key"=>$hash_key) );
    $author     = $em->getRepository('SciForumVersion2Bundle:SubmissionAuthor')->findOneBy( array("id"=>$author_id, "hash_key"=>$hash_key) );
    if( $author == null ) {
        $author = new SubmissionAuthor();
        $author->setPerson( new Person() );
    }

    $enquiry    = new Affiliation();
    $formType   = new SubmissionAffiliationFormType();
    $form       = $this->createForm($formType, $enquiry);

    $request    = $this->getRequest();
    $valid      = true;
    $error      = '';
    if( $request->get('cancel') == 'yes' ) return $this->redirect($this->generateUrl('SciForumVersion2Bundle_user_submission', array("key"=>$submission->getHashKey())));
    if ($request->getMethod() == 'POST' && $request->get('ajax') == 'no') {

        $form->bindRequest($request);

        if ($valid = $form->isValid()) {

            if( $valid ) {

                $em->persist($enquiry);
                $em->flush();

                $uAff = new UserAffiliation();
                $uAff->setUserId( $user->getId() );
                $uAff->setAffiliationId( $enquiry->getId() );
                $uAff->setUser( $user );
                $uAff->setAffiliation( $enquiry );
                $em->persist($uAff);
                $em->flush();

                // Redirect - This is important to prevent users re-posting
                // the form if they refresh the page
                return $this->redirect($this->generateUrl('SciForumVersion2Bundle_user_submission', array("key"=>$submission->getHashKey())));
            }
        }
  • 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-14T00:43:39+00:00Added an answer on June 14, 2026 at 12:43 am

    Your issue is that you’re returning a full new HTML content.
    You need to return only informations that your JS can understand and treat the way you want.

    use Symfony\Component\HttpFoundation\Response;
    

    This class in SF allows you to return plain content.

    Here’s a way to use it.

    if ($valid) {
        $data = array(
            'success' => true,
            'responseCode' => 307, /* Redirect Temp */
            'redirectURI' => $this->generateUrl('SciForumVersion2Bundle_user_submission', array("key"=>$submission->getHashKey())),
            'message' => '<h3>You\'re about to be redirected...</h3>'
        );
    } else {
        $data = array(
            'success' => false,
            'responseCode' => 400, /* Bad Request */
            'message' => '<h3 class="error">Cannot send form<br>* Field NAME is empty</h3>'
        );
    }
    $json = json_encode($data);
    /* Response($data,$statusCode,$additionnalHeaders) */
    return new Response($json,200,array('Content-Type'=>'application/json'));
    

    Then in your JS

    $.ajax({
        url: "uri/to/controller",
        type: "POST",
        data: values, /* Sent data */
        cache: false,
        success : function(data,e,xhr){
            if (xhr.status == 200) {
                if (data.responseCode == 200) { /* Success, just a message */
                    $("#content .contentwrap .itemwrap")
                        .addClass("success")
                        .html(data.message);
                } else if (data.responseCode == 307) { /* Redirection */
                    $("#content .contentwrap .itemwrap")
                        .addClass("success")
                        .html(data.message);
                    setTimeout(function(){
                        $(window).attr("location",data.redirectURI);
                    },3000);
                } else if (data.responseCode == 400) { /* Invalid Form */
                    $("#content .contentwrap .itemwrap")
                        .addClass("error")
                        .html(data.message);
                } else { /* Could not understand Response Code */
                    $("#content .contentwrap .itemwrap")
                        .html("<h3>Technical issue occured. Please come back later.</h3>");
                }
            } else {
                $("#content .contentwrap .itemwrap")
                    .html("<h3>Technical issue occured. Please come back later.</h3>");
            }
        }
    })
    

    You could imagine any parameter to pass through $data

    I used HTTP/1.1 codes to talk with Javascript but you can use anything you want
    Ex:

    'action' => 'redirect'
    'youAreGoingTo' => 'printAwesomeMessage'
    

    JS

    if (data.action == 'redirect')
    
    if (data.youAreGoingTo == 'printAwesomeMessage')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue in that a Symfony2 form can pass validation but still
(I'm using Symfony2) I have a form that asks for the name and the
I have an entity type form field in my Symfony2 project. $builder = $this->createFormBuilder();
I have a problem to upload an image using Symfony. I have a form
I don't know why, but I remembered that when you have a symfony form
I am using many to many relations in my symfony2 project, i have successfully
I have added a second submit button to my form, now Symfony2 complains about
I have category_id field in my Symfony2 form. After calling $form->bindRequest($this->request) I want to
I have a custom form type for a Rating entity in my Symfony2 project.
I have a Symfony2 form with a variety of fields, including one optional text

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.