J PHP AJAX PHP AJAX
<?phpheader("Content-Type:html/text;Charset=utf-8");$name = $_POST['name'];$email = $_POST['email'];$comment = $_POST['comment'];$fh = @fopen("14-6.txt","ab");fwrite($fh,"姓名:".$name,strlen($name));fwrite($fh,"email:".$email,strlen($email));fwrite($fh,"评论:".$comment,strlen($comment));fclose($fh);echo 1;?>
function $(id){ return document.getElementById(id);}function addcomment(){ var url = "14-6.php"; var status = document.getElementById("divmsg"); status.value="正在提交》》》"; var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value; xmlhttp_request.onreadystatechange = function(){ if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息 alert (xmlhttp_request.responseText); if(xmlhttp_request.responseText == "1"){ status.value = "发表成功!!"; $("name").value=""; $("email").value=""; $("comment").value=""; }else{ status.value = "发表失败!请重新发表!"; } } } xmlhttp_request.open("POST",url,true); xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息 xmlhttp_request.send(param);}
var param = "name="+$("name").value+"email="+$("email")。 value+ "comment="+$("comment").value;
var param = "name="+$("name").value+" &email="+$("email").value+ と記述する必要があります。 " &comment= "+$("comment").value;
私のものはHTMLファイルからPHPファイルへのものです。
HTMLコードは以下です。 HTML ファイルと php ファイルの合計 2 つのファイルしかありません
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Insert title here</title><script type="text/javascript">var xmlhttp_request = false; //定义变量try{ if(window.ActiveXObject){ //判断浏览器是否是ActiveXObject对象 for(var i=5;i;i--){ //根据IE浏览器版本使用不同方法创建 try{ if(i==2){ xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP"); }else{ xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP."+i+".0"); xmlhttp_request.setRequestHeader("Content-Type","text/xml"); xmlhttp_request.setRequestHeader("Charset","utf-8"); } break; }catch(e){ xmlhttp_request = false; } } }else if(window.XMLHttpRequest){ //判断浏览器是否支持XMLHttpRequest对象 xmlhttp_request = new XMLHttpRequest(); if(xmlhttp_request.overrideMimeType){ xmlhttp_request.overrideMimeType('text/xml'); } }}catch(e){ xmlhttp_request = false;}function $(id){ return document.getElementById(id);}function addcomment(){ var url = "14-6.php"; var status = document.getElementById("divmsg"); status.value="正在提交...."; var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value; xmlhttp_request.onreadystatechange = function(){ if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息 alert(xmlhttp_request.responseText); if(xmlhttp_request.responseText == "1"){ status.value = "发表成功!!"; $("name").value=""; $("email").value=""; $("comment").value=""; }else{ status.value = "发表失败!请重新发表!"; } } } xmlhttp_request.open("POST",url,true); xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息 xmlhttp_request.send(param);}</script></head><body><form name="form" id="form" method="post" action=""> <p> </p> <p> </p> <p> </p> <p> </p> <table width="400" height="168" border="0" align="center" cellpadding="2" cellspacing="0"> <caption>发表评论</caption> <tr> <td >用户名:</td> <td align="center"> <lable> <input type="text" name="name" id="name" size="30" maxlength="20"/> </lable> </td> </tr> <tr> <td >email:</td> <td align="center"> <lable> <input type="text" name="email" id="email" size="30" maxlength="20"> </lable> </td> </tr> <tr> <td>评论:</td> <td align="center"> <textarea name="comment" id="comment" rows="6" cols="30"></textarea> </td> </tr> <tr> <td colspan="2" align="center"> <lable> <input type="button" name="submit" value="发表" id="submit" onclick="addcomment();"/> </lable> <lable> <input id="divmsg" name="divmsg" value="" readonly /> </lable> </td> </tr> </table> </form></body></html>