Reputation: 7
i called a controllerAction in symfony2 from jquery script in twig template for form validation and verification. After a successful response i called another controller from jquery script using $.post() for rendering a template for user-dashboard. but all i am getting is a template displayed in console but not in the user browser window. How to achieve this in the above stated way. ??
Thanks for help (in advance) AKSHAT.
<script>
// my jquery code
$(document).ready(function()
{
$('.sign_in_box').click(function()
{
var data = new Object();
data.email=$('.email_txt').val();
data.password=$('.pwd_txt').val();
$.ajax('{{ path('login_login_validation') }}', {
type : 'POST',
data : data,
success : function(response){
var json = JSON.parse(response);
var flag = json.success;
if(flag)
{
$('#pwd_login_error').css('opacity' , '0');
$('#email_login_error').css('opacity' , '0' );
$.ajax('{{ path('login_login_verification') }}',{
type : 'POST',
data : data,
success : function(response){
var login = JSON.parse(response);
if(login.login)
{
alert("login success");
$.post('{{ path('login_dashboard') }}', data);
}
else
{
$('#pwd_login_error').css('opacity' , '0.5' );
}
}});
}
else
{
$('#pwd_login_error').css('opacity' , '0' );
$('#email_login_error').css('opacity' , '0.5' );
}
}
});
});
});
</script>
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// controller
<?php
namespace Login\Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DashboardController extends Controller
{
public function indexAction()
{
$email = $this->get('request')->get('email');
return $this->render('LoginBundle:Dashboard:index.html.twig', array('email'=> $email));
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// routing file
login_homepage:
pattern: /
defaults: { _controller: LoginBundle:Homepage:index }
login_login_validation:
pattern: /ajax_validate
defaults: { _controller: LoginBundle:Login:validation }
login_login_verification:
pattern: /ajax_verify
defaults: { _controller: LoginBundle:Login:verification }
login_dashboard:
pattern: /dashboard
defaults: { _controller: LoginBundle:Dashboard:index }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// twig file
{% block message %}
hello ! {{ email | json_encode | raw }}
welcome to dashboard .
{% endblock%}
Upvotes: 0
Views: 122
Reputation: 935
The only wrong thing is this your are rendering the index page . after successfull login you have to redirect you page by
window.location = "http://somewhereelse.com";
Or You can also append data in any div , Its on to you which way you want to follow .
Upvotes: 1