Ajax & PHP 边学边练 之四 表单
通过上一篇文章已经了解到如何利用Ajax和PHP对数据库进行数据读取,这样可以动态的获取到数据库的最新数据。本篇则继续介绍通过表单(Form)向数据库中写入数据。 function processajax (serverPage,obj,getOrPost,str){ //将创建XMLHttpRequest对象写到getxmlhttp()函数中,并获取该对象 xmlhttp = getxmlhttp (); //GET方式(和前面几篇一样) if (getOrPost == "get"){ xmlhttp.open("GET",serverPage); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ obj.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); } //POST方式 else{ //第三个true参数将打开异步功能 xmlhttp.open("POST",serverPage,true); //创建POST请求 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=GB2312"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { obj.innerHTML = xmlhttp.responseText; } } //表单(Form)传值 xmlhttp.send(str); } } 在下图中当点击“Submit”按钮后会激发submitform函数(functions.js),在该函数中会通过getformvalues函数检查Form内容是否都填写完毕,否则提示哪项未填写。当检查通过后会调用process_task.php程序,它会将Form值写入数据库。
源码:Sample5.rar 作者博客:http://www.cnblogs.com/gnielee/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |