-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #3411 [Cookbook][Dynamic Form Modification] Add AJAX sample (…
…bicpi) This PR was merged into the 2.3 branch. Discussion ---------- [Cookbook][Dynamic Form Modification] Add AJAX sample | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.3+ | Fixed tickets | #2464 ("AJAX experience") Do we need a PHP version of the `create.html.twig` which would mainly contain redundant JavaScript? Commits ------- a75ad9c Shorten AJAX example ee33dcd Remove some empty lines from code samples f47a7c3 Updates & Fixes after public review 2533f29 [Cookbook][Dynamic Form Modification] Add AJAX sample
- Loading branch information
Showing
2 changed files
with
120 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<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> |