-2

我是 php 新手,我想知道是否有人可以帮助我,基本上我已被分配创建一个数据库,其中有四个问题和四个答案,数据库应该显示四个问题,当你点击按钮时,它会说“不正确” 或“正确” 在数据库上如果这有意义这里是我的 html 代码但我不知道如何让 php 将其放入数据库中,我尝试使用 Div/ID 标签来帮助但我没有知道我在做什么

<img src="Images/Question1.png" usemap="#mainMap" id="main" style="position:absolute;display:none;left:0px;top:0px;" border="0" />
<map name="mainMap" id="mainMap">
  <div id="Incorrectanswer1"><area shape="rect" coords="82,192,196,242" onclick="incorrectAnswer()" /></div>
  <div id="Incorrectanswer2"><area shape="rect" coords="83,254,197,300" onclick="incorrectAnswer()" /></div>
  <div id="Incorrectanswer3"><area shape="rect" coords="83,310,201,368" onclick="incorrectAnswer()" /></div>
  <div id="Correctanswer1"><area shape="rect" coords="84,373,205,430" onclick="correctAnswer()" /></div>
</map>

如果有人可以帮助或指出我正确的方向,这将很有帮助。

4

1 回答 1

1

As some people has suggested you should change the way this is designed.

HTML/Javascript is client side and a simple right-click -> show source can tell any user which is the correct answer. So what is the correct way to do it? well there are some, let me explain one of them:

Your HTML part is fine, but the onclick part should be changed to a new function called check_answer(id) for example, you pass the answer id to that function, then it will be submited to a PHP that will check it display a text telling the user if the answer was correct or not.

One way to do this would be AJAX, but let's try one more simple:

<form id="answer_send" name="answer_send" action="check_answer.php" method="POST">
     <input type="hidden" id="answer_id" />
</form>

This HTML can be inserted in any part of the body, it won't be displayed anyway. Then we need the new javascript function 'check_answer(id)':

function check_answer(id){
    document.getElementById('answer_id').value=id;
    document.forms["answer_send"].submit();
}

Then in the check_answer.php:

$id=$_POST['answer_id'];

if($id > 0){
     //do something to compare the answer
     if(answer_ok(id)){
         echo "Correct Answer!";
     }else{
         echo "Incorrect Answer!";
     }
}

What's the problem here? the user would get redirected to another page, so if you want to display multiple questions/answers and check them without sending the user to another URL you should use AJAX. The only difference when doing this process with AJAX would be that there's no need for a form, and the check_answer(id) function would have to do the AJAX call and display a message to the user depending on the return value of the AJAX call.

于 2012-12-03T15:26:12.580 回答