php - Checking Correct Answer and Submitting Form -


I'm not proficient in PHP because I hope that I have most of my experience with Wordpress loops.

I am trying to create a very simple quiz that performs the number of correct answers in the URL (e.g., Domaindotcom /? P = 3 if they have found 3 correct answers yet).

I'm using the following PHP code to get started:

  & lt ;? Php / * Current correct answer count * / $ answer_count = $ _GET ["p"]; / * Check to see if the submitted response is similar to the correct answer / ($ _POST ["submit-answer"] == "correct-answer") {$ answer_count ++; }? & Gt;  

Now I know that I can get the right link using the link:

  & lt; A href = "link-to-next-question.php /? P = & lt ;? php echo $ answer_count;? & Gt;" & gt; Next question & lt; / A & gt;  

But now I am trying to use it in a form and confuse with post, GET, etc.

Here's my HTML:

  & lt; Form name = "quiz" action = "" method = "post" & gt; & Lt; Label = "o1"> gt; & Gt; Input type = "radio" name = "grp" id = "o1" value = "o1" & gt; Label 1 & lt; / Labels & gt; & Lt; Label = "o2"> gt; & Gt; Input type = "radio" name = "grp" id = "o2" value = "o2" & gt; Label 2 & lt; / Labels & gt; & Lt; Label = "o3" & gt; & Lt; Input type = "radio" name = "grp" id = "o3" value = "o3" & gt; Label 3 & lt; / Label & gt; & Lt; Input type = "submit" value = "next question" class = "btn" & gt; & Lt; / Form & gt; How do I choose the right answer (security is not important, it's just a fun quiz) and after adding the increment to the $ answer_count before the URL, you sent it to the next URL. has gone?   

Do not pass the answer number in the link (i.e. by GET) instead of just a hidden form field (S) and use the client-side javascript code to increase the variable and submit the form.

  & lt; Form id = "qfrm" name = "quiz" verb = "" method = "post" & gt; & Lt; Input type = "hidden" name = "question_number" value = "& lt ;? php echo $ next_question_number?" Gt; & Gt; & Lt; Input type = "hidden" id = "n_c_a" name = "num_correct_answers" value = & lt ;? Php echo $ num_correct_answers? & Gt; "& Gt; Input type =" button "value" = "submit" onclick = "validateAnswerAndSubmit (); returned false;" gt; & Lt; / Form & gt; & Lt; Script language = "javascript" & gt; Validate the function: Onusbus () {if (valid_ answer ()) {document.getElementById ("n_c_a"). Value + = 1;} document. GetElementById ("qfrm"). Then submit your php script switch to  $ _ POST ["question_number"]  


OK, you do not want to use javascript ... if you are actually asking "How do I know which radio box is selected from PHP? " The answer is:

  & lt ;? Php $ answer = $ _POST ["grp"] ;? & Gt;  

I think that you will actually have two variable URL, one question_number, and the second number will be. You can then type the code like this:

  & lt; Php $ num_correct = (int) $ _GET ["num_correct"]; $ Question_number = (int) $ _GET ["question_number"]; Switch ($ question_number - 1) {// according to the conference considering your question number // validate the answer to the previous question -1: // No verification required to break the first question ($ question_number 0); Case 0: If ($ _ POST ["grp"] == "correct answer") {$ num_correct ++; }     break; // And so on; }? & Gt; & Lt; Form name = "quiz" action = "this_page.php /? Num_correct = & lt ;? php echo $ num_correct ;? & gt; question_number = & lt; php $ question_number + 1? & Gt; method =" post "& Gt; & lt; Php Display_Question_Number ($ question_number) ;? & gt; & lt; / form & gt;  

The main point here is that in the" action = "anchor "Href =", that is, this is the URL on which the form is stored when the user clicks the submit button.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -