简体   繁体   中英

form with multiple questions and answers

I want to store question with multiple choice answers. The amount of answers can differ from 2 to more answers.

I have a few questions for this. But first I will show my current code:

html:

<div class="questionFormTemplate">
    <form action="utils/handleForm.php" method="post">
        <table>
            <tr>
                <td><input type="text" name="question" value="question?"/></td>                                
            </tr>
            <tr>
                <td><input type="text" name="answer[]" value="answer 1"/></td>
                <td><input type="button" value="remove" class="remove"/></td>                                
            </tr>
            <tr>
                <td><input type="text" name="answer[]" value="answer 2"/></td>
                <td><input type="button" value="remove" class="remove"/></td>
            </tr>            
            <tr>
                <td><input type="button" value="add answer"/></td>
            </tr>
        </table>
        <input type="submit" value="update" />  
    </form>
</div>

<div id="container"></div>

js / jquery:

// add some question forms to the container
var questionFormTemplate = $('.questionFormTemplate').html();

console.log(questionFormTemplate);

for(var i = 0; i < 8; i++) {
   $('.container').append(questionFormTemplate);
}


// make the buttons work
$('input[type="button"][value="remove"]').on("click", function() {
    $(this).parent().parent().remove();
});

$('input[type="button"][value="add answer"]').on("click", function() {
    $(this).parent().parent().parent().append('<tr><td><input type="text" name="answer[]" value="answer"/></td><td><input type="button" value="remove" class="remove"/></td></tr>');
});

css:

.questionFormTemplate {
    display: none;
}

http://jsfiddle.net/clankill3r/5nkunpsw/3/

1) Is it easier to make a form for every question, or one form with all questions?

2) How can I know which question is sended to handleForm.php (as in 1, 2 or 3 etc.)

3) Is my current approach good or bad?, I need to be able to display what's in the database as well.

Well one thing I would suggest is giving each problem an id attribute which you can then send to your php class which can validate from the database. For example for form 1 question 2 you could give it "id=12".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM