<html> <head> <title>ThaiCreate.Com JSP Tutorial</title> </head> <body> <script type="text/javascript"> function fncSubmit() { if(document.form1.txt1.value == "") { alert('Please input Input 1'); document.form1.txt1.focus(); return false; } if(document.form1.txt2.value == "") { alert('Please input Input 2'); document.form1.txt2.focus(); return false; } document.form1.submit(); } </script> <form action="page.cgi" method="post" name="form1" onSubmit="JavaScript:return fncSubmit();"> Input 1 <input name="txt1" type="text"><br> Input 2 <input name="txt2" type="text"><br> <input name="btnSubmit1" type="submit" value="Submit" > </form> </body> </html>
<html> <head> <title>ThaiCreate.Com JSP Tutorial</title> </head> <body> <% String strInput1 = "Name"; String strInput2 = "Surname"; %> <script type="text/javascript"> function fncSubmit() { if(document.form1.txt1.value == "") { alert('Please input <%=strInput1%>'); document.form1.txt1.focus(); return false; } if(document.form1.txt2.value == "") { alert('Please input <%=strInput1%>'); document.form1.txt2.focus(); return false; } document.form1.submit(); } </script> <form action="page.cgi" method="post" name="form1" onSubmit="JavaScript:return fncSubmit();"> <%=strInput1%> <input name="txt1" type="text"><br> <%=strInput2%> <input name="txt2" type="text"><br> <input name="btnSubmit1" type="submit" value="Submit" > </form> </body> </html>
<html> <head> <title>ThaiCreate.Com JSP Tutorial</title> </head> <script type="text/javascript"> var HttPRequest = false; function doCallAjax() { HttPRequest = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... HttPRequest = new XMLHttpRequest(); if (HttPRequest.overrideMimeType) { HttPRequest.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { HttPRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { HttPRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (!HttPRequest) { alert('Cannot create XMLHTTP instance'); return false; } var url = 'post.jsp'; var pmeters = 'myName=' + document.getElementById("txtName").value; //var pmeters = 'myName='+document.getElementById("txtName").value+'&my2='; // 2 Parameters HttPRequest.open('POST', url, true); HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); HttPRequest.setRequestHeader("Content-length", pmeters.length); HttPRequest.setRequestHeader("Connection", "close"); HttPRequest.send(pmeters); HttPRequest.onreadystatechange = function() { if (HttPRequest.readyState == 3) // Loading Request { document.getElementById("mySpan").innerHTML = "Now is Loading..."; } if (HttPRequest.readyState == 4) // Return Request { document.getElementById("mySpan").innerHTML = HttPRequest.responseText; } } /* HttPRequest.onreadystatechange = call function .... // Call other function */ } </script> <body> <input type="text" name="txtName" id="txtName" value=""> <input name="btnButton" id="btnButton" type="button" value="Click" onClick="JavaScript:doCallAjax();"> <br> <span id="mySpan"></span> </body> </html>
<% out.print("Sawatdee : " + request.getParameter("myName")); %>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท