Page 1 of 1

Simple matching script with MySQL and HTML forms help

Posted: Thu Aug 10, 2006 10:22 pm
by DillyDong
Hello, all, I've got a question I hope you can help me out with.

I've got a database with two columns, one is called `the_question`, and the other is `the_answer`. Say the database looks like this:

Code: Select all

+++++++++++++++++++++++++++++
|`the_question`|`the_answer`|
|++++++++++++++++++++++++++++
|Apple         | Red        |
|Banana        | Green      |
|Grape         | Purple     |
|Blueberry     | Blue       |
|Cherry        | Red        |
+++++++++++++++++++++++++++++
Now, what I want to do is have a form on a page where on the left-hand column, each value from `the_question` is listed, and on the right of each `the_question` value, there should be an input box where the user should enter the appropriate answer.
Now, I want to pass this data to a new page via form action, and I want the next page to check the user's responses against the appropriate `the_answer`fields. I'm having trouble understanding exactly how to do this. The script I have for the first page is:

test_1.php

Code: Select all

<form method="post" action="test_2.php">
<table>
<tr><td>Apple</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Banana</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Grape</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Blueberry</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Cherry</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
</table>
<input type="submit" value="Submit" />
</form>
test_2.php

Code: Select all

<?php
foreach($_POST as $value) {
$data = $_POST; }

foreach($data as $data_for_query) {
$sql = "SELECT * FROM `table` WHERE `the_answer` = ".$data_for_query.";";
$query = mysql_query($sql);
if(empty(mysql_num_rows($query))) {
echo "Sorry, the correct answer was: " ;
//And at this point, how do I get it to return the correct answer? 
//All I passed through the form was user input 
else {
echo "Good job.";
}
}
?>
Any help would be greatly appreciated! I have no idea.

Posted: Thu Aug 10, 2006 11:29 pm
by matt1019
Hi DillyDong:

Code: Select all

<form method="post" action="test_2.php"> 
<table> 
<tr><td>Apple</td><td><input type="text" name="(don't know what to put here)" /></td></tr> 
<tr><td>Banana</td><td><input type="text" name="(don't know what to put here)" /></td></tr> 
<tr><td>Grape</td><td><input type="text" name="(don't know what to put here)" /></td></tr> 
<tr><td>Blueberry</td><td><input type="text" name="(don't know what to put here)" /></td></tr> 
<tr><td>Cherry</td><td><input type="text" name="(don't know what to put here)" /></td></tr> 
</table> 
<input type="submit" value="Submit" /> 
</form>
every single line that has

Code: Select all

name="(don't know what to put here)"
should be a unique name.... such as:

name="question1"

These are the values that will be "Posted" by your first page, and these are the same values that the second page will get.
(someone correct me if I am wrong)

Hope this helps.

-Matt

Posted: Thu Aug 10, 2006 11:31 pm
by DillyDong
Matt, thanks for the reply. While it's true you need a unique name for each input box to pass to the next form, I'm still looking for a way to check those values against a database and return feedback to the user as stated.

Posted: Thu Aug 10, 2006 11:34 pm
by matt1019
actually, looking at your second part of the code, test_2.php, i just realized that you have a missing parenthesis before else{

should be }else{


I am taking a look at your code now.

let me see what I can do.

-Matt

Posted: Fri Aug 11, 2006 12:37 am
by RobertGonzalez

Code: Select all

<?php
// Since the form has them and the answers need them,
// grab them here and use them throughout
$qa_array = array();

// Set a few other vars for use later
$form = '';

$sql = 'SELECT `the_question`, `the_answer` FROM `mytable`';
if ( !$result = mysql_query($sql) )
{
    die('Could not get the Q and A list: ' . mysql_error());
}

if ( mysql_num_rows($result) )
{
    $a = 0;
    while ( $row = mysql_fetch_array($result) )
    {
        $row['the_id'] = $a;
        $qa_array[] = $row;
        ++$a;
    }
}
$qa_count = count($qa_array);

/**
 * Ok, now the array is set.
 * Check if there is a posted form,
 * if so, process the post
 * if not, show the form
 **/
if ( isset($_POST['form_sent']) && $_POST['form_sent'] == 'true' )
{
    // Process the form
    foreach ($_POST as $key => $value)
    {
        if ( is_numeric($key) )
        {
            if ( $value == $qa_array[$key]['the_answer'] )
            {
                // The answer to the question matches what is in the DB
            }
            else
            {
                // The answer does not match the answer in the DB
            }
        }
    }
}
else
{
    // Show the form
    $form = '<form id="myform" method="post" action="' . basename(__FILE__) .'">';
    for ($a = 0; $a < $qa_count; ++$a)
    {
        $form .= '<p>' . $qa_array[$a]['the_question'] . ':<br /><input type="text" name="' . $qa_array[$a]['the_id'] . '" size="30" /></p>';
    }
    $form .= '<input type="hidden" name="form_sent" value="true" />';
    $form .= '<input type="submit" name="submit" value="Send this form" />';
    $form .= '</form>';

    echo $form;
}

Posted: Fri Aug 11, 2006 8:57 am
by DillyDong
Wow! Thanks for the incredibly detailed response. Quick question before I try to implement it - how would the code change if I wanted the form and the processing parts of the script separated into two different files (with action="file_2.php"), because as of right now, the first file with the form on it already has much data passed through a form script on a previous page, and I don't want those to get confused :/

Posted: Fri Aug 11, 2006 10:52 am
by RobertGonzalez
Change the action part of the form to point to your new page, then take this part and add it to your new page to process the posted stuff...

Code: Select all

<?php
// Since the form has them and the answers need them,
// grab them here and use them throughout
$qa_array = array();

// Set a few other vars for use later
$form = '';

$sql = 'SELECT `the_question`, `the_answer` FROM `mytable`';
if ( !$result = mysql_query($sql) )
{
    die('Could not get the Q and A list: ' . mysql_error());
}

if ( mysql_num_rows($result) )
{
    $a = 0;
    while ( $row = mysql_fetch_array($result) )
    {
        $row['the_id'] = $a;
        $qa_array[] = $row;
        ++$a;
    }
}
$qa_count = count($qa_array);

/**
 * Ok, now the array is set.
 * Check if there is a posted form,
 * if so, process the post
 * if not, show the form
 **/
if ( isset($_POST['form_sent']) && $_POST['form_sent'] == 'true' )
{
    // Process the form
    foreach ($_POST as $key => $value)
    {
        if ( is_numeric($key) )
        {
            if ( $value == $qa_array[$key]['the_answer'] )
            {
                // The answer to the question matches what is in the DB
            }
            else
            {
                // The answer does not match the answer in the DB
            }
        }
    }
}