views:

45

answers:

2

Please, show me how to get a random question, '$randomQuestion'(i.e., "What color is the sky?"), and matching answer, $matchingAnswer, from the quizID section of my SQL database, and then use jQuery to refresh the form, only? I started by creating the form and some JavaScript that may work.

FORM:

<form name="$quizID" action="http://asite.com" method="post">
  <fieldset>
    <legend="$randomQuestion">
    <p>
      <label>Answer: <input type="text" id="answer" onkeydown="submitAns(submit.id)" /></label>
    </p>
  </fieldset>
</form>

JS:

function submitAns(id) {
  if (document.getElementById(id).value=="$matchingAnswer")
          document.a.submit();
}

JQUERY:

$.post('get-question_matchinganswer_for_quizID.php', {
  quizID: $quizID,
  question },
  function(data) {
    alert('Question is: ' + data.question)
    alert('Answer is: ' + data.answer)
  },
  'json'
);

PHP:

<!-- Help -->
function random_row($table, $column) {
  $max_sql = "SELECT max(" . $column . ") 
          AS max_id
          FROM " . $table;
  $max_row = mysql_fetch_array(mysql_query($max_sql));
  $random_number = mt_rand(1, $max_row['max_id']);
  $random_sql = "SELECT * FROM " . $table . "
          WHERE " . $column . " >= " . $random_number . " 
          ORDER BY " . $column . " ASC
          LIMIT 1";
  $random_row = mysql_fetch_row(mysql_query($random_sql));
  if (!is_array($random_row)) {
    $random_sql = "SELECT * FROM " . $table . "
            WHERE " . $column . " < " . $random_number . " 
            ORDER BY " . $column . " DESC
            LIMIT 1";

    $random_row = mysql_fetch_row(mysql_query($random_sql));
  }
  return $random_row;
}

$randomQuestion =
$matchinAnswer =

This is extremely complicated for me, and I've been having a world of trouble with it. PLEASE, comment. Thank you.

+1  A: 

Well to start with, your HTML is invalid: You're not closing your tags:

<form name="$quizID" action="http://asite.com" method="post">
    <fieldset>
        <legend><?php echo $randomQuestion?></legend>
        <label>
            Answer: <input type="text" id="answer"
                           onkeydown="submitAns(submit.id)" />
        </label>
    </fieldset>
</form>

As for the ajax, I would suggest using jQuery. It makes things much easier.

$.post('getqidqandanswer.php', {
    quizID: 1337,
    questionID: 42},
    function(data) {
        alert('Question is: ' + data.question)
        alert('Answer is: ' + data.answer)
    },
    'json'
);

PHP:

$quizID = isset($_POST['quizID']) ? $_POST['quizID'] : null
$questionID = isset($_POST['questionID']) ? $_POST['questionID'] : null

if($quizID && $questionID)
{
   $data = getQuestionData($quizID, $questionID)
}
elseif($quizID)
{
   $data = getRandomQuestionData($quizID)
}
else
{
    $data = array(
        'question' => '',
        'answer'   => ''
    )
}

echo json_encode($data)
Eric
My bad. The w3 doc, http://www.w3.org/TR/html5/forms.html#writing-a-form-s-user-interface, shows <p> surrounding the label. I am not sure if it aesthetic or not. So, I hope it's okay to be cautious?
Fohsap
The `<p>` was fine. I just removed it because it was unnecesary. The issue was the unclosed `<label>` and `<fieldset>` tags.
Eric
Using jQuery would be fine. The trouble I'm having is 'how do I check the values against the database, submit, and still not refresh the page, without pressing the 'submit' button?' XD It's supposed to submit when the Answer in the text input matches the answer listed in the DB, which is associated with the Q in the DB.
Fohsap
@Marcel: Actually, I think that that's a valid use. It surprised me the first time I saw it too.
Eric
I'm not even sure <input> is supposed to be closed, though label, legend, and fieldset definitely should be. I'm looking at 4.10 of the forms doc (linked above), and they don't close it there.
Fohsap
Input is self-closing, at least in XHTML. Although in HTML, it _is_ valid not to close it.
Eric
Thanks for the help, so far. I'll repost the question and request it with jQuery, rather than ajax.
Fohsap
Just edit the question. AJAX is a method of javascript usage. jQuery is a javascript library that does ajax.
Eric
May I ask one quick question? Why do you use questionID: 42 and quizID: leet? Would the questionID be random if it were just questionID (without 42)?
Fohsap
About `input` wrapped inside of `label`: yes, you're right, it *is* valid, so the rendering engine can determine the input element the label belongs to. About self-closing: according to the HTML 4.01 spec, the end tag is [forbidden](http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT).
Marcel Korpel
@Fohsap: If you remove the questionID (leaving `{quizID: 1337}`), then the PHP code will generate a random question.
Eric
Thanks, Eric. You rock. :D
Fohsap
A: 

Are you sure AJAX is what you're looking for? Are the question and answer going to change within an instance of the page, or are they determined on the first load of the page?

Eric