ตัวอย่างโค้ด ajax_post.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < script language = "javascript" src = "ajax.js" ></ script > < script language = "javascript" > function doajax(mydata){ var ajax1=createAjax(); ajax1.onreadystatechange=function(){ if(ajax1.readyState==4 && ajax1.status==200){ document.getElementById('myplace').innerHTML=ajax1.responseText; }else{ return false; } } ajax1.open("POST","data_post.php",true); ajax1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajax1.send("name="+document.form1.name.value+"&email="+document.form1.email.value); } </ script > < form action = "" method = "post" enctype = "multipart/form-data" name = "form1" id = "form1" > ชื่อ < input name = "name" type = "text" id = "name" /> อีเมลล์ < input name = "email" type = "text" id = "email" /> < input type = "button" name = "Button" value = "Send" onclick = "doajax()" /> </ form > < p id = "myplace" ></ p > |
โค้ด data_post.php
1 2 3 4 5 6 7 8 9 10 | <?php header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false); if ( $_POST [ 'name' ]!= "" ){ echo $_POST [ 'name' ]. " " ; } if ( $_POST [ 'email' ]!= "" ){ echo $_POST [ 'email' ]; } ?> |
ดาวน์โหลด ajax.js คลิกดาวน์โหลด
ดูตัวอย่าง คลิกดูตัวอย่าง