dsaxw4201 2010-09-12 16:20
浏览 40
已采纳

请帮助我获得$ randomQ和$ matchinA,然后如果键入$ matchinA则刷新jQuery?

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.

  • 写回答

2条回答 默认 最新

  • dousheng3364 2010-09-12 16:48
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 软件工程用例图的建立(相关搜索:软件工程用例图|画图)
  • ¥15 如何在arcgis中导出拓扑关系表
  • ¥15 处理数据集文本挖掘代码
  • ¥15 matlab2017
  • ¥15 在vxWorks下TCP/IP编程,总是connect()报错,连接服务器失败: errno = 0x41
  • ¥15 AnolisOs7.9如何安装 Qt_5.14.2的运行库
  • ¥20 求:怎么实现qt与pcie通信
  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3