这是一款基于php实现的猜单词小游戏,用的SESSION配合AJAX实现的,感兴趣的小伙伴可以参考一下。
直接复制本地运行就可以了
<script>
function Ajax(recvType){
var aj=new Object();
ajrecvType=recvType ? recvTypetoUpperCase() : 'HTML';
ajtargetUrl='';
ajsendString='';
ajresultHandle=null;
ajcreateXMLHttpRequest=function(){
var xmlHttp = false;
if(windowXMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else if(windowActiveXObject){
try{
xmlHttp = new ActiveXObject("MsxmlXMLHTTP");
}catch(error){
try{
xmlHttp = new ActiveXobject("MicrosoftXMLHttp");
}catch(error){
xmlHttp = false;
}
}
}
return xmlHttp;
}
ajXMLHttpRequest=ajcreateXMLHttpRequest();
ajprocessHandle=function(){
if(ajXMLHttpRequestreadyState == ){
if(ajXMLHttpRequeststatus == ){
if(ajrecvType=="HTML"){
ajresultHandle(ajXMLHttpRequestresponseText);
}else if(ajrecvType=="XML"){
ajresultHandle(ajXMLHttpRequestresponseXML);
}
}
}
}
ajget=function(targetUrl, resultHandle){
ajtargetUrl=targetUrl;
if(resultHandle!=null){
ajXMLHttpRequestonreadystatechange=ajprocessHandle;
ajresultHandle=resultHandle;
}
if(windowXMLHttpRequest){
ajXMLHttpRequestopen("get", ajtargetUrl);
ajXMLHttpRequestsend(null);
}else{
ajXMLHttpRequestopen("get", ajtargetUrl, true);
ajXMLHttpRequestsend();
}
}
return aj;
}
</script>
<script>
var ajax=Ajax();
function select(v){
documentgetElementById(v)styledisplay='none';
ajaxget("guessphp?op=ajax&k="+v, function(r){
var t=rsplit("|");
documentgetElementById('word')innerHTML=t[];
if(t[] == ){
documentgetElementById('select')styledisplay='none';
documentgetElementById('result')innerHTML='成功';
}else if(t[] == ){
documentgetElementById('select')styledisplay='none';
documentgetElementById('result')innerHTML='失败';
}
});
}
function check(v){
ajaxget("guessphp?op=start&k="+v, function(r){
windowlocationhref="<?php echo $url;?>";
});
}
</script>
easy: wrong
';
echo ' normal: wrong
';
echo ' hard: wrong
';
}else{
//echo $word;
echo "最多可以猜错 "$_SESSION['num']" 次";
echo "
";
echo "
这是一款php实现的猜单词游戏,希望大家可以举一反三,实现其他小游戏,,熟练掌握php编程。