验证提交给数据库的答案

时间:2016-01-25 15:52:21

标签: javascript php database validation

<?php
include ("dbFunctions.php");


$query = "SELECT * FROM two_three";

$result = mysqli_query($link, $query);

mysqli_close($link);
?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="CSS/main.css" type="text/css"/>
    <link rel="stylesheet" href="CSS/easy.css" type="text/css"/>

    <title> Easy Game 1 </title>
<center>
    <h1> Easy Game 1 </h1>
    <h2> Click the image the number of times to get the answer! </h2>
    <div class="border_solid">
        <div id="timer"></div>
    </div>
    <hr>
</center>
</head>
<body>
<center>
<form method="post" id="validate">
    <div id="display"><script type="text/javascript">document.write(count);</script></div>
    <img src="image/ufo.png" class="ufo" onclick="add()"><br>
    <input type="button" class="submit" value="submit" name="submit"/>
</form>
</center>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
    $questID = $row['two_three_id'];
    $factor1 = $row['factor1'];
    $factor2 = $row['factor2'];
    $answer = $row['answer'];
    ?>
    <div class="image-questionid"><u>Question <?php echo $questID; ?></u></div>
    <div class="image-questionfactor1"><?php echo $factor1; ?></div>
    <div class="image-betweenfactor">X</div>
    <div class="image-questionfactor2"><?php echo $factor2; ?></div>
    <div class="image-questionequals">= ?</div>
    <?php
    break;
    }
}
?>
</body>
<script type="text/javascript">
var count = 0;
function add() {
    count++;
    document.getElementById('display').innerHTML = count;
}

var myVar = setInterval(function () {
    myTimer()
}, 1000);
var d = 0;
function myTimer() {
    document.getElementById("timer").innerHTML = d++;
}
</script>
</html>

所以基本上这是我到目前为止所做的代码,我想在用户点击提交后进行验证以检查计数,并使用数据库答案检查计数。那里有谁可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

表单中的

使用标签形式的方法onsubmit =&#34; validation&#34;。在你需要在javascript中实现验证方法并将ajax请求(在验证中)发送到另一个php表单之后,在那个php表单中你将查询发送到db并返回你需要做的点击次数。 o你需要实现jquery库。

function validation(){
$.ajax({
  url: "your_php_requesting_to_db.php",
  success: function(data){
   if(data == numberofclicks){
     return true;
   }else{
     return false;
   }

  }
});

}

答案 1 :(得分:0)

这是新代码,我在这个foro中是新的,所以我不知道如何对代码进行评论,我知道你得到了你需要做的总点击,回答你的问题这里是一点点文档

http://www.w3schools.com/js/js_timing.asp

<?php
    include ("dbFunctions.php");
    $query = "SELECT * FROM two_three";
    $result = mysqli_query($link, $query);
    mysqli_close($link);
?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="CSS/main.css" type="text/css"/>
    <link rel="stylesheet" href="CSS/easy.css" type="text/css"/>
    <title> Easy Game 1 </title>
    <center>
        <h1> Easy Game 1 </h1>
        <h2> Click the image the number of times to get the answer! </h2>
        <div class="border_solid">
            <div id="timer"></div>
        </div>
        <hr>
    </center>
</head>
<body>
<center>
<form method="post" id="validate"  onsubmit="validate()">
    <div id="display">
        <script type="text/javascript">document.write(count);</script>
    </div>
    <img src="image/ufo.png" class="ufo" onclick="add()"><br>
    <input type="hidden" id="totalClicks" value="<?php /*IF U GOT THE NUMBER OF CLIC FROM BD PUT IN THERE LIKE ECHO $NUMCLICKS */ ?>"/>
    <input type="hidden" id="countClic" value="0" />
    <input type="button" class="submit" value="submit" name="submit"/>
</form>
</center>
<?php
    if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $questID = $row['two_three_id'];
        $factor1 = $row['factor1'];
        $factor2 = $row['factor2'];
        $answer = $row['answer'];
        ?>
        <div class="image-questionid"><u>Question <?php echo $questID; ?></u></div>
        <div class="image-questionfactor1"><?php echo $factor1; ?></div>
        <div class="image-betweenfactor">X</div>
        <div class="image-questionfactor2"><?php echo $factor2; ?></div>
        <div class="image-questionequals">= ?</div>
        <?php
        break;
        }
    }
?>
</body>
<script type="text/javascript">
var count = 0;
function add() {
    count++;
    document.getElementById('display').innerHTML = count;
    document.getElementById('countClic').value=count;
}

var myVar = setInterval(function () { 
    myTimer() 
}, 1000);
var d = 0;
function myTimer() {
    document.getElementById("timer").innerHTML = d++;
}
function validate(){
    var totalClics = document.getElementById('totalClicks').value=count;
    var clicksCount = document.getElementById('countClic').value=count;
    if(totalClics == clicksCount){
        return true;
    }else{
        return false;
    }
}
</script>
</html>