为什么最后一个正确答案仍然有效?

时间:2016-10-23 06:44:12

标签: javascript php ajax

这是一个测验网站,但是当我提交答案并且其正确弹出一个新问题但正确答案和最后正确答案有效(最后正确无效,直到新的最后答案)我已经尝试了几个小时来修复它并看到没有解决方案...最后正确答案不是假设使用新问题

var right = 0
function start(){
	var name = $('input#answer').val()
	if ($.trim(name) != ''){
		$.post('ajax/name.php', {name: name}, function(data){
			$('#correctbox').text(data)
		if (name == data){
			rightfunc()
			rand()
		}else{
			rand()
		}
		} )
	}
}
function rand(){
		$.post('ajax/test.php', {name: name}, function(data){
			$('#questionsbox').text(data)
			document.getElementById("answer").value = '';
		} )
}
function rightfunc(){
	right = right + 1
	rightbox.innerHTML = "<b>Correct | "+right+"</b>"
}
<?php
if (isset($_POST['name']) === true && empty($_POST['name']) === false){

	require '../db/connect.php';
	mysql_set_charset('utf8');
	$name = mysql_real_escape_string(strip_tags(addslashes(trim($_POST['name']))));
	$query = ("SELECT *
	FROM japanese 
	WHERE Romanji = '$name'
	")or die(mysql_error());
	
	$response = mysql_query($query);
   
	if($response === false){
		die(mysql_error());
	}
	while($row = mysql_fetch_array($response)){
		if($name == $row['Romanji']){
			echo ($row['Romanji']);
		}else{
			echo ('Name not found');
		}
	}
}
?>

1 个答案:

答案 0 :(得分:0)

当您给出正确答案时,不要清空input#answer

.post('ajax/name.php', {name: name}, function(data){
            $('#correctbox').text(data);
            //add next line so you can remove it from rand()
            document.getElementById("answer").value = '';
        if (name == data){
            rightfunc()
            rand()
        }else{
            rand()
        }
        } )

另一件事是为什么你总是向数据库发送相同的查询?如果您的问题发生变化,您的查询应该不同?