0

How can I refresh a page after an ajax call is executed. My Ajax call is

 $.ajax({
         type: "POST",
         data: data,
         url:"{{ path('v2_pm_patents_trashpatents') }}",
         cache: false
});

The method that is executed by this ajax is

 public function trashpatentAction(Request $request){
    if ($request->isXmlHttpRequest()) {
        $patent_id = $request->get("pid");
        $em = $this->getDoctrine()->getEntityManager();
        $patent = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents')->find($patent_id);
        if (!$patent) {
            throw $this->createNotFoundException('No patent found for id '.$patent_id);
        }
        $patent->setIs_deleted(1);
        $em->flush();
    }
}

Edit

Portfolio Controller Index Action

public function indexAction(Request $request) {
     $switch_form = $this->createForm(new SwitchPortfolioType($portfolios));
     ////rest of action code
     return $this->render('MunichInnovationGroupPatentBundle:Portfolio:index.html.twig', array('new_patentgroup_form' => $new_patentgroup_form->createView(),'switch_form' => $switch_form->createView(), 'new_form' => $new_form->createView(), "portfolios" => $portfolios ,"selected_portfolio" => $selected_portfolio,"portfolio_groups" => $portfolio_groups,"patents" => $patents));

}

Index.html.twig

 <form name="portfolios" action="{{ path('v2_pm_portfolio') }}" method="post" >
            {{ form_widget(switch_form) }}
            <input type="submit"class="portfolio_input button2 tooltip" value="Switch">
 </form> 

SwitchPortfolioType

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class SwitchPortfolioType extends AbstractType
{
public function __construct($portfolios)
{
    $this->portfolios = $portfolios;
}


public function buildForm(FormBuilder $builder, array $options)
{
    $builder
    ->add('', 'choice', array(
        'choices'   => array(
                'test' => 'Test'
        ),
            'empty_value' => 'Switch your Portfolio',
    ));
}

public function getName()
{
    return 'switch_portfolio';
}

}

At the end of this action I want to refresh the page

How can I do this?

j0k
  • 22,600
  • 28
  • 79
  • 90
Zoha Ali Khan
  • 1,659
  • 11
  • 37
  • 56

2 Answers2

2

The refresh will have to be done in javascript. I am not sure why you would POST via ajax only to reload the page anyway, why not just POST normally?

Anyway, to do what you ask I would return some simple JSON response in the controller to signify a success and then in the ajax callback do this:

$.ajax({
     type: "POST",
     data: data,
     url:"{{ path('v2_pm_patents_trashpatents') }}",
     cache: false,
     success: function(data) {
         // optionally check if the response is what you wanted
         //if (data.response == 'deleted') {
             document.location.reload(true);
         //}
     }
});
MDrollette
  • 6,887
  • 1
  • 36
  • 49
  • I've edited my post to change the reload function. Seems this one should be more universally acceptable to reloading the page. from here: http://stackoverflow.com/questions/2624111/preferred-method-to-reload-page-with-javascript – MDrollette Jun 24 '12 at 20:25
  • it says Variable "switch_form" does not exist in "MunichInnovationGroupPatentBundle:Portfolio:index.html.twig" – Zoha Ali Khan Jun 24 '12 at 20:29
  • That's unrelated. That means you're trying to use a variable in twig that was not passed in via the controller. – MDrollette Jun 24 '12 at 20:36
  • I would need to see more code from the controller action and the twig template to guess why it's not visible. – MDrollette Jun 24 '12 at 20:43
0

Using jQUery:

$('your_form').disable=true;

Pure JS:

your_form.disabled = true;
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Andy.Diaz
  • 3,369
  • 2
  • 22
  • 24