PHP表单 - 如果选择了2个单选按钮

I have a very simple HTML form on a PHP page for a questionnaire.

10 yes/no questions. Radio buttons.

I am trying to figure out how to show a hidden div if any 2 of the radio buttons are set to 'yes'.

would i be better off using a jquery script, or PHP?

PHP could only do it if you roundtrip the form through the server after every button click, which'd still require Javascript to detect the click and submit the form for revamping.

Remember that PHP runs on the server. By the time the user sees the form and is clicking around on it, PHP has long since completed its job and has shut down.

In other words, you need to use Javascript for this.

If you want to do it without submitting the form, you have to do it with Javascript. If you don't mind another roundtrip, you can do it with PHP.

You're not "better off" either way. It depends on what you want to do and how important the feature is.

With JS you can store the number of selected "yes" options in a variable and as soon as this variable is 2 or higher, you toggle the additional content div.

With PHP you analyze the $_POST array, count the "yes" answers and send the same page to the client including the additional div plus the already selected answers.

You'd probably be better off using jQuery and handling it client side.

Something like:

$(function () {
    $(".radioClass").change(function () {
        // check through all radio buttons for YES / NO
        // toggle div visibility appropriately
    });
});