#4 OK da: Jeg har lavet et eksempel.
AjaxPostFormUpdate.php er den rutine, der modtager formfelterne. I min demo her aflæses de bare med php og returneres som en streng pakket ind i XML;
Det er her din nuværende databehandling/opdatering skal være.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
http://www.w3.org/TR/html4/strict.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Ajax Post Demo</title>
<style type="text/css">
.t1 { border-collapse: collapse; border: lightgray }
td {
padding: 2px 4px 2px 4px ;
}
</style>
<script type="text/javascript">
var msgArea;
var Form1Request;
function AjaxRequest() {
var xhr;
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP"); // Trying Internet Explorer
}
catch(e) // Failed
{
try {
xhr = new XMLHttpRequest(); // Other browsers.
}
catch(e2) {
xhr = null;
}
}
return xhr;
}
function SendForm1Ajax() {
msgArea.nodeValue="Please Wait";
var param = "firstname=";
param += document.getElementById("Form1FirstName").value;
param += "&lastname=";
param += document.getElementById("Form1LastName").value;
Form1Request.onreadystatechange = Form1Reply;
Form1Request.open("POST", "AjaxPostFormUpdate.php", true);
Form1Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
Form1Request.send(param);
}
function Form1Reply() {
msgArea.nodeValue += "..";
if(Form1Request.readyState == 4) {
if(Form1Request.status == 200) {
var reply = Form1Request.responseXML.getElementsByTagName("reply")[0].firstChild.nodeValue;
msgArea.nodeValue=reply;
}
else
msgArea.nodeValue="Error " + Form1Request.status + " - " + Form1Request.statusText;
}
}
function init() {
msgArea = document.getElementById("MsgAreaID").firstChild;
Form1Request = new AjaxRequest();
if (Form1Request)
msgArea.nodeValue=String.fromCharCode(160);
else
msgArea.nodeValue="Error: Your Browser does not support XMLhttpRequest";
}
</script>
</head>
<body onload="init();">
<p id="MsgAreaID" style="color: red; text-size; 150%">
Error: Javascript not active
</p>
<table id="demoTable" class="t1" border="1">
<tr>
<td>First Name</td>
<td><input type="text" id="Form1FirstName"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" id="Form1LastName"></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Send" onclick="SendForm1Ajax()"></td>
</tr>
</table>
<p>
<textarea rows="3" cols="40">What you type here remains on screen, because the form above is sent via a XMLhttpRequest and this page does not reload
</textarea>
</p>
</body></html>