Note that there are some explanatory texts on larger screens.

plurals
  1. PORouting for Symfony2 TWIG templates with AJAX
    primarykey
    data
    text
    <p>I have been struggling with this for a couple days and have yet to find any real solutions online yet. I have a form that allows users to enter their email, then after they enter it fades out and is replaced by another form. I am using ajax to submit data from a form to a symfony2 controller, which stories it in the database and sends a response. However, the response ends up just sending me to a blank page with the response data displayed, instead keeping me on the same page and just fading the boxes as needed. I think my issue is with how I have the controller actions set up and the routing files. </p> <p><strong>EDIT</strong></p> <p>Well now the issue is that when I click on the submit button nothing happens. Nothing is sent to the controller, so nothing is being stored and no response is being given. What I changed was based on the answer by @PaulPro, appending the </p> <pre><code>&lt;script&gt; $('#emailForm').submit(submitForm); &lt;/script&gt; </code></pre> <p>to the end of the html page. Thanks in advance for any help and insight!</p> <p>The controller has 2 relevant actions, the one that renders the TWIG Template with the form, and the one that handles the ajax form submission and returns the response. </p> <pre><code>public function emailAction() { return $this-&gt;render('Bundle:email.html.twig'); } public function submitEmailAction() { $em = $this-&gt;getDoctrine()-&gt; getEntityManager(); $email = new Email(); $request = $this-&gt;getRequest(); $emailVar = $request-&gt;request-&gt;get('emailSignup'); $email -&gt; setEmail($emailVar); $em-&gt;persist($email); $em-&gt;flush(); $return=array("responseCode"=&gt;200); $return = json_encode($return); return new Response($return, 200, array('Content-Type'=&gt;'application/json')); } </code></pre> <p>This is the routing for those actions, most likely the culprit of it all:</p> <pre><code>Bundle_email: pattern: /email defaults: { _controller: Bundle:email } requirements: _method: GET Bundle_submitEmail: pattern: /submitEmail defaults: { _controller: Bundle:submitEmail } requirements: _method: POST </code></pre> <p>And then here is the email.html.twig template and ajax script:</p> <pre><code>&lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;link href="{{asset('css/styles.css')}}" rel="stylesheet" type="text/css" /&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function submitForm(){ var emailForm = $(this); var path = "{{path('Bundle_submitEmail')}}" $.ajax ( { type: 'POST', url: $("#emailForm").attr("action"), emailSignup: $("#emailId").val(), success: function(data){ if(data.responseCode==200 ){ $('.email-signup-form').fadeOut(); $('.share-form').delay(500).fadeIn(); } } }); return false; }&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="emailForm" action="{{path('Bundle_submitEmail')}}" method="POST" class="email-signup-form"&gt; &lt;input type="email" placeholder="e-mail address" name="emailSignup" id="emailId" required="true"/&gt; &lt;input type="submit" value="Go"&gt; &lt;/form&gt; &lt;form class="share-form"&gt; &lt;/form&gt; &lt;script&gt; $('#emailForm').submit(submitForm); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload