求教:怎么回答这样的面试题(简易随机问答验证:在php存储问题和答案,在htm中随机读取问题后,判断用户输入的答案是否正确)?特地来请教了!谢谢大家了! 本帖最后由 amhoho 于 2014-04-03 16:57:15 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码不完整,仅给你点思路。剩下的自己做吧$user_answer = $_GET['user_answer'];$items = [['question' => '一年有多少天?', 'anwser' => '365'], ['question' => '一天有几个小时?', 'anwser' => '24']];$seq = rand(0, count($items)-1);$question = $items[$seq]['question'];$answer = $items[$seq]['anwser']; 这不是招人的面试题吗?我也搞不来 所以来请教学习 这和抄有关系?和我的饭碗又有什么关系?说起思路 我自己是用用php里数组 html中传递value判断 a.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <title> question and answer </title> </head> <body> <p id="question"></p> <p>answer:<input type="text" id="answer"></p> <p><input type="submit" value="submit" id="smb"></p> <input type="hidden" id="index"> <script type="text/javascript"> $(document).ready(function(){ $.getJSON("a.php?type=get", function(data){ $('#question').html(data['question']); $('#index').val(data['index']); }); }) var lock = 0; $('#smb').click(function(){ if($.trim($('#answer').val())==''){ alert('please input answer'); return false; } lock = 1; $.getJSON("a.php?type=check&answer=" + encodeURIComponent($('#answer').val()) + "&index=" + encodeURIComponent($('#index').val()), function(data){ if(data.ret==true){ alert('answer is correct'); }else{ alert('answer is incorrect'); } lock =0; }); }); </script> </body></html>a.php<?php$type = isset($_GET['type'])? $_GET['type'] : '';$question = array( array('一年有多少天','365天'), array('一天有几个小时?','24小时'), array('1+1=?','2'), array('2+2=?','4'), array('3+3=?','6'), array('4+4=?','8'), array('5+5=?','10'), array('6+6=?','12'), array('7+7=?','14'), array('8+8=?','16'), array('9+9=?','18'),);if($type=='get'){ $index = mt_rand(0, count($question)-1); echo json_encode(array('question'=>$question[$index][0],'index'=>$index));}elseif($type=='check'){ $answer = isset($_GET['answer'])? $_GET['answer'] : ''; $index = isset($_GET['index'])? $_GET['index'] : '-1'; $result = false; if(isset($question[$index])){ if($question[$index][1]==$answer){ $result = true; } } echo json_encode(array('ret'=>$result));}?> @fdipzone 不错 我的思路差不多也是这样写的 嘿 要是我这的 来当同事吧 哈哈哈 请教一个mysql复合查询的问题 挺难~ 求一个正则和截取函数 惊天大发现!!! utf-8与UTF-8等等的编码问题? mysql读取中文标点时数据被截断 strip_tags函数的返回值 ----PHP 页面反复刷新后退后.JS无法运行---- 这些函数我测试的结果怎么和他说的不一样? 关于apache+php+mysql配置问题 继续请教mysql查询语句:多表联合查询,如何限定右侧的表如果有对应记录则加入特定的where限制或者limit限制,反之则不加限制 iis和php的整合后出现这样的问题 如何将数据库查询的记录集 赋值给表格
$answer = $items[$seq]['anwser'];
说起思路 我自己是用用php里数组 html中传递value判断
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<title> question and answer </title>
</head> <body>
<p id="question"></p>
<p>answer:<input type="text" id="answer"></p>
<p><input type="submit" value="submit" id="smb"></p>
<input type="hidden" id="index"> <script type="text/javascript">
$(document).ready(function(){
$.getJSON("a.php?type=get", function(data){
$('#question').html(data['question']);
$('#index').val(data['index']);
});
})
var lock = 0;
$('#smb').click(function(){
if($.trim($('#answer').val())==''){
alert('please input answer');
return false;
}
lock = 1;
$.getJSON("a.php?type=check&answer=" + encodeURIComponent($('#answer').val()) + "&index=" + encodeURIComponent($('#index').val()), function(data){
if(data.ret==true){
alert('answer is correct');
}else{
alert('answer is incorrect');
}
lock =0;
});
});
</script>
</body>
</html>a.php<?php
$type = isset($_GET['type'])? $_GET['type'] : '';$question = array(
array('一年有多少天','365天'),
array('一天有几个小时?','24小时'),
array('1+1=?','2'),
array('2+2=?','4'),
array('3+3=?','6'),
array('4+4=?','8'),
array('5+5=?','10'),
array('6+6=?','12'),
array('7+7=?','14'),
array('8+8=?','16'),
array('9+9=?','18'),
);if($type=='get'){
$index = mt_rand(0, count($question)-1);
echo json_encode(array('question'=>$question[$index][0],'index'=>$index));
}elseif($type=='check'){
$answer = isset($_GET['answer'])? $_GET['answer'] : '';
$index = isset($_GET['index'])? $_GET['index'] : '-1'; $result = false; if(isset($question[$index])){
if($question[$index][1]==$answer){
$result = true;
}
} echo json_encode(array('ret'=>$result));
}
?>