Skip to content

Instantly share code, notes, and snippets.

@morontt
Created October 7, 2014 07:38
Show Gist options
  • Select an option

  • Save morontt/476d097e377b6402dc34 to your computer and use it in GitHub Desktop.

Select an option

Save morontt/476d097e377b6402dc34 to your computer and use it in GitHub Desktop.
Preview action
<?php
class QuestionController extends BaseController
{
/**
* @param Request $request
* @return JsonResponse
*/
public function saveSessionAction(Request $request)
{
$question = new Question();
$form = $this->createForm(new QuestionType(), $question);
$form->submit($request);
$result = false;
if ($form->isValid()) {
$questionArray = $question->toArray();
$this->getSession()->set('question', $questionArray);
$result = true;
}
return new JsonResponse($result);
}
/**
* @Template()
*
* @return array
*/
public function previewAction()
{
return [
'question' => $this->getSession()->get('question'),
];
}
}
$(function () {
var other_window = false;
$('button.preview').click(function () {
$.ajax({
url: 'path/to/question_save_session',
data: $('form.question-form').serialize(),
dataType: 'json',
type: 'POST',
success: function (data) {
if (data) {
if (other_window) {
other_window.location.reload(true);
} else {
other_window = window.open('/path/to/question_preview');
}
}
}
});
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment