I would like to create a form in Symfony2 which displays a set of radio buttons depending on the selected value in a combo-box belonging to the same form.
I have not been able to find a suitable solution in Symfony2 that would allow me to have this functionality.
Is there any way to use the Symfony2 framework at least for part of the implementation or should I generate the form manually?
I would like to create a form in Symfony2 which displays a set of
Share
What you’re looking for is AJAX. This can be implemented with straight javascript or jQuery (jQuery is much cleaner and easier to use).
Here’s a jQuery stub for you to get an idea of what has to happen:
If you’re not familiar with AJAX & jQuery, I’ll explain what’s going on.
$('#mySelect')is almost the equivalent of javascript’sgetElementById()function. They are NOT the same functionality (jQuery uses the same notations as css to find the elements you’re looking for), but this will return the element withmySelectas its id attribute..change()sets the onChange event handler to thefunction()defined within the brackets.$.postis jQuery shorthand for performing an AJAXpost(as opposed to aget), that is sent to backgroundScript.php without a page load. When backgroundScript.php returns with a 200 status (i.e. “everything’s OK, here’s my output”) it will perform the callback functionfunction(data).backgroundScript.php will receive
valas a $_POST variable and do “something” to determine which radio buttons should be displayed, then will write output to the screen.EDIT (more info):
backgroundScript.php should look something like this if you’re determining the radio buttons based on a database. Keep in mind that this is not written with security or consideration of deprecated functions in mind (e.g.
mysql_*), just functionality: