用AJAX实现发表评论的添加与显示

  • Post author:
  • Post category:其他


<script>

<!–

/*

初始化一个xmlhttp对象

*/

function InitAjax()

{


var ajax=false;

try {

ajax = new ActiveXObject(“Msxml2.XMLHTTP”);

} catch (e) {

try {

ajax = new ActiveXObject(“Microsoft.XMLHTTP”);

} catch (E) {

ajax = false;

}

}

if (!ajax && typeof XMLHttpRequest!=’undefined’) {

ajax = new XMLHttpRequest();

}

return ajax;

}

/*提交*/

function saveUserInfo()

{

if(document.frm_comment.comment.value==””)

{


alert(‘请输入评论的内容!’);

return false;

}

//获取接受返回信息层

var msg = document.getElementById(“ComList”);

//alert(‘www’);

//获取表单对象和用户信息值

var f = document.frm_comment;

var strWriter = f.strWriter.value;

var strPassword = f.strPassword.value;

var comment = f.comment.value;

var indent = f.indent.value;

var MusicID = f.MusicID.value;

//接收表单的URL地址

var url = “AddComment.asp?idWriter=7715780&Key=495320293”;

//需要POST的值,把每个变量都通过&来联接

var postStr = “indent=”+ escape(indent) +”&content=”+ escape(comment) +”&strWriter=”+ escape(strWriter)+”&strPassword=”+

escape(strPassword) +”&MusicID=”+ escape(MusicID);

//实例化Ajax

var ajax = InitAjax();

//通过Post方式打开连接

ajax.open(“POST”, url, true);

//定义传输的文件HTTP头信息,这一行不能少哦^_^

ajax.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);

//发送POST数据

ajax.send(postStr);

//获取执行状态

ajax.onreadystatechange = function() {

//如果执行状态成功,那么就把返回信息写到指定的层里

if (ajax.readyState == 4 && ajax.status == 200) {

msg.innerHTML = ajax.responseText;

}

}

document.frm_comment.comment.value=””

}


function checkform()

{


if(document.frm_comment.comment.value==””)

{


alert(‘留言内容不能为空!’);

return false;

}

}

–>

</script>

<script language=”javascript”>

function checkform()

{


if (document.form_login.user_login_name.value==”)

{


alert(“请输入登录名!”);

document.form_login.user_login_name.focus();

return false;

}

if (document.form_login.user_login_password.value==”)

{


alert(“请输入密码!”);

document.form_login.user_login_password.focus();

return false;

}

}

</script>

<div id=”msg” name=”msg”></div>

<!–发表评论开始 –>

<form name=”frm_comment” id=”frm_comment” method=”post” action=”” οnsubmit=”return checkform()”>

作者:<font color=>Wintalen</font><input type=’hidden’ name=’strWriter’ value=’Wintalen’><input type=’hidden’ name=’strPassword’  value=”>

<table width=”72%” border=”0″ cellspacing=”0″ cellpadding=”0″>

<tr>

<td style=”padding:0 10px 10px 10px;”>

<textarea name=”comment” cols=”60″ rows=”5″></textarea>

</td>

</tr>

<tr>

<td style=”padding-right:10px; text-align:right”>

段首缩进两个汉字:<input type=”checkbox” name=”indent” value=”1″ checked>

<input type=”hidden” name=”MusicID” value=”73″>

<input name=”submit1″ type=”button” id=”submit1″ value=”提交” οnclick=”saveUserInfo()” class=”inputButton”>

</td>

</tr>

</table>

</form>

<!–发表评论结束 –>



版权声明:本文为Wintalen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。