做之前不用异步直接测试HTML和PHP都是没问题的(说明下,我的后台用的PHP),但是当我采用AJAX异步上传的时候就出现问题了,本人新手,出于初级学习阶段,希望大家帮我指出下问题。
另外我想向WEB开发方向学习,目前只是熟悉了HTML,css,php(不过库函数没记到几个,基本都是w3c开着写代码),JS刚刚接触,学了一段时间后,我发现自己最基本HTTP协议都没有去看过,请问这样是不是有点本末倒置呢?希望有人能给指出个学习的顺序和重点,感激不尽。
下面上代码
html表单部分(表单具体内容太多,无关紧要,只贴了3个重要部分)
- HTML code
<html> <head> <title>上传页面</title> <script type="text/javascript" src="js/upload.js"></script> </head> <body> <form id="uploadform" method="post" ENCTYPE="multipart/form-data"> <input onclick="show()" type="submit" value="上传" /> </form> <div style="width:200px;height:80px;background:yellow; float:left" id="uploadshow"></div> </body>
upload.js(大致就是先请求一张拥有处理表单数据和上传文件的页面,获取处理后的值再请求一张数据库存信息的php页面,那张页面先检测文件是否上传成功,成功就讲数据存入数据库)
- JScript code
function createAjax(){ var request=false; //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE8) if(window.XMLHttpRequest){ request=new XMLHttpRequest(); if(request.overrideMimeType){ request.overrideMimeType("text/xml"); } //window对象中有ActiveXObject属性存在就是IE }else if(window.ActiveXObject){ var versions=['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP']; for(var i=0; i<versions.length; i++){ try{ request=new ActiveXObject(versions[i]); if(request){ return request; } }catch(e){ request=false; } } } return request; } var ajax=null; function show(){ ajax=createAjax(); ajax.open("post", "program_upload.php",true); ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajax.send(document.getElementById("uploadform").submit()); //指定异步处理显示对象 var obj_uploadshow=document.getElementById("uploadshow"); //回调函数 ajax.onreadystatechange=function(){ obj_uploadshow.innerHTML='uploading..'; if(ajax.readyState==4 && ajax.status==200){ var file=ajax.responseText; eval("var obj="+file); var id = obj.id; var program_contentadd = obj.program_contentadd; var program_date = obj.program_date; var program_team = obj.program_team; var program_title = obj.program_title; var program_author = obj.program_author; var program_add = obj.program_add; var program_type = obj.program_type; function upload_sql(){ ajax=createAjax(); ajax.open("POST", "program_upload_sql.php",true); ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajax.send('id='+id+'&program_contentadd='+program_contentadd+'&program_date='+program_date+'&program_team='+program_team+'&program_title='+program_title+'&program_author='+program_author+'&program_add='+program_add+'&program_type='+program_type); if(ajax.responseText==null){ var continue_send; continue_send=setTimeout("upload_sql()",1000);//每1秒检测一次文件是否上传成功 } var obj_uploadshow=document.getElementById("uploadshow"); ajax.onreadystatechange= function(){ if(ajax.readyState==4 && ajax.status==200 && ajax.responseText!=null){ var result=ajax.responseText; obj_uploadshow.innerHTML = "chenggong";//result; } } } } else{ alert("shibai"); } } }