Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MySQL "Quiz" Game - Form Input Value
    primarykey
    data
    text
    <p>Alright, so I've been working on this for two days now - my code is somewhat sloppy &amp; jumbled but I've gone over hundreds of questions, websites, etc. etc. looking for an answer or simply an explanation I understood; unfortunately, I still have been unsuccessful in my attempts.</p> <p>I am build a "Quiz" Game in PHP/HTML - the website references a database, specifically, a tabled labeled "answers" which holds the following information:</p> <pre><code> - ID: Auto-Increment - Question: Varchar - Answer: Varchar - Comment: Varchar </code></pre> <p>Now, for a little information on the site - Once a user logs in, he/she can "play" the game; the game is simply an HTML form, which above it displays a random "answers table" question. The form has 4 user inputs but only requires two. Let me get into the code details and then I will ask my question:</p> <p>My index.php page (which contains the game form) is currently:</p> <pre><code>&lt;?php # index.php session_start(); //check session first if (!isset($_SESSION['email'])){ include ('../includes/header.php'); }else { session_start(); include ('../includes/header.php'); require_once ('../../mysql_connect.php'); $query = "SELECT * FROM answers ORDER BY RAND() LIMIT 1"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num &gt; 0) { // If it ran OK, display all the records. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ?&gt; &lt;div class="newGame"&gt; &lt;h2&gt;Are you a Question Master?&lt;hr /&gt;&lt;/h2&gt; &lt;h3 style="color:#000"&gt;Find Out Now!&lt;/h3&gt; &lt;/div&gt; &lt;br /&gt; &lt;div class="newGameContain"&gt; &lt;form action="gameSubmit.php" method="post" autocomplete="off"&gt; &lt;h2&gt;&lt;? echo $row["Question"]."&lt;hr /&gt;"; ?&gt;&lt;/h2&gt; &lt;h3&gt;Enter Player Answers&lt;/h3&gt; &lt;p&gt;&lt;input type="text" placeholder="Player 1" name="player1" value="&lt;? echo $_POST['player1']; ?&gt;" /&gt; &lt;input type="text" placeholder="Player 2" name="player2" value="&lt;? echo $_POST['player2']; ?&gt;" /&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="text" placeholder="Player 3" name="player3" value="&lt;? echo $_POST['player3']; ?&gt;" /&gt; &lt;input type="text" placeholder="Player 4" name="player4" value="&lt;? echo $_POST['player4']; ?&gt;" /&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="submit" class="submitButton" /&gt; &lt;input type="reset" class="resetButton" value="Reset" /&gt; &lt;/p&gt; &lt;input type="hidden" name="ID" value="&lt;?php echo $row["ID"]; ?&gt;" /&gt; &lt;input type="hidden" name"Answer" value="&lt;?php echo $row['Answer']; ?&gt;" /&gt; &lt;input type="hidden" name="submitted" value="TRUE" /&gt; &lt;/form&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt; &lt;br /&gt; &lt;?php } //end while statement } //end if statement mysql_close(); //include the footer include ("../includes/footer.php"); } ?&gt; </code></pre> <p>Then my gameSubmit.php page (form action) looks like this - I will only give a snapshot, not the whole thing:</p> <pre><code> &lt;?php # index.php session_start(); //check session first if (!isset($_SESSION['email'])){ include ('../includes/header.php'); }else { session_start(); include ('../includes/header.php'); require_once ('../../mysql_connect.php'); $query = "SELECT * FROM answers ORDER BY RAND() LIMIT 1"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num &gt; 0) { // If it ran OK, display all the records. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ?&gt; &lt;? if (isset($_POST['submitted'])){ $correct1Msg = "&lt;div class='correct1Msg'&gt;&lt;p style='color:#000;font-family:Arial, Helvetica, sans-serif;'&gt;Player 1 entered the &lt;span id='answerUnder'&gt;correct answer&lt;/span&gt;.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;"; $correct2Msg = "&lt;div class='correct2Msg'&gt;&lt;p style='color:#000;font-family:Arial, Helvetica, sans-serif;'&gt;Player 2 entered the &lt;span id='answerUnder'&gt;correct answer&lt;/span&gt;.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;"; $incorrect1Msg = "&lt;div class='incorrect1Msg'&gt;&lt;p style='color:#F00;font-family:Arial, Helvetica, sans-serif;'&gt;Player 1 entered the &lt;span id='answerUnder'&gt;incorrect answer&lt;/span&gt;.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;"; $incorrect2Msg = "&lt;div class='incorrect2Msg'&gt;&lt;p style='color:#F00;font-family:Arial, Helvetica, sans-serif;'&gt;Player 2 entered the &lt;span id='answerUnder'&gt;incorrect answer&lt;/span&gt;.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;"; $player1Answer = $_POST['player1']; $player2Answer = $_POST['player2']; $player3Answer = $_POST['player3']; $player4Answer = $_POST['player4']; $questionID = $row['ID']; if ($questionID == "1" &amp;&amp; $player1Answer != "Red"){ echo $incorrect1Msg; }elseif ($questionID == "2" &amp;&amp; $player1Answer != "4"){ echo $incorrect1Msg; }else { echo $correct1Msg; } if ($questionID == "1" &amp;&amp; $player2Answer == "Red"){ echo $correct2Msg; }elseif ($questionID == "2" &amp;&amp; $player2Answer == "4"){ echo $correct2Msg; }else{ echo $incorrect2Msg; } } ?&gt; &lt;?php } //end while statement } //end if statement mysql_close(); //include the footer include ("../includes/footer.php"); } ?&gt; </code></pre> <p>As a note, the gameSubmit.php page also has identical message and if...elseif... statements for player3Answer &amp; player4Answer.</p> <p>So my question is...</p> <p>If a user is logged in and opens the index.php page, he/she is prompted with the "echo $row ["Question"]" (which is a question pulled from the MySQL database using $query = "SELECT * FROM answers ORDER BY RAND() LIMIT 1"; - The user then proceeds to enter an answer in each player's respective text input. Once the user clicks the submit button, the form redirects to gameSubmit.php - once loaded, if(isset($_POST['submitted'])){ launches and cross checks each users answer and displays the respective message.</p> <p>Currently, my form redirects to gameSubmit.php, however, it doesn't reference the previous question for the correct answer - thus its sheer luck the identical answer appears when "grading" the answers.</p> <p>What do I need to do/what needs to be corrected in order to achieve input validation on the form action page?</p> <p>Once again, I simply want to retrieve a question at random and on submit check the inputted answers with the correct answer - I would also like my code to be able to retrieve the correct answer rather than me having to type out each answer, so that way, if a record gets added, I dont have to update the code.</p> <p>Thank for your time and the help, it is much appreciated! (It's finals week and I couldn't be more stressed)</p> <ul> <li>Rockmandew</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload