Skip to content

Commit

Permalink
Dynamic form modification cookbook: Fix inclusion of code
Browse files Browse the repository at this point in the history
Includes in a code block don't work, replace them with the actual code
  • Loading branch information
Michaël Perrin committed Mar 20, 2014
1 parent 41b2eb8 commit 8093719
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
52 changes: 50 additions & 2 deletions cookbook/form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,31 @@ field according to the current selection in the ``sport`` field:
{# ... #}
{{ form_end(form) }}

.. include:: /cookbook/form/dynamic_form_modification_ajax_js.rst.inc
<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function(){
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>

.. code-block:: html+php

Expand All @@ -664,7 +688,31 @@ field according to the current selection in the ``sport`` field:
<!-- ... -->
<?php echo $view['form']->end($form) ?>

.. include:: /cookbook/form/dynamic_form_modification_ajax_js.rst.inc
<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function(){
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>

The major benefit of submitting the whole form to just extract the updated
``position`` field is that no additional server-side code is needed; all the
Expand Down
25 changes: 0 additions & 25 deletions cookbook/form/dynamic_form_modification_ajax_js.rst.inc

This file was deleted.

0 comments on commit 8093719

Please sign in to comment.