Det kan du sagtens. Du kunne jo skrive noget à la:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
http://www.w3.org/TR/html4/strict.dtd"><html>
<head>
<title>Ajax Eksempel :: [
http://www.eksperten.dk/spm/817625]</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/JavaScript">
(function(){if(window.XMLHttpRequest)return;var o=null,s,a=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(var i=0,j=a.length;i<j;s=a[i],i++){try{if(o=new ActiveXObject(s))break}catch(e){}}window.XMLHttpRequest=o?function(){return new ActiveXObject(s)}:null;o=null})();
function AjaxReq(sMethod, sUrl, oVars, fnCallBack) {
var oHttp = new XMLHttpRequest();
oHttp.open(sMethod, sUrl, true);
oHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
oHttp.onreadystatechange = _fnCallBack;
var aQuery = [];
for (var x in oVars) aQuery.push( encodeURIComponent(x) + "=" + encodeURIComponent(oVars[x]) );
oHttp.send( aQuery.join("&") );
aQuery = null;
fnCallBack()
function _fnCallBack() {
if (oHttp.readyState<4) return;
fnCallBack(oHttp);
oHttp = null;
}
return this;
}
function myCallBack(oHttp) {
document.getElementById("msgDispl").firstChild.nodeValue = oHttp.responseText;
}
function sendAjaxReq(oF) {
var oVars = {};
oVars.context = oF.context.value;
oVars.navn = oF.navn.value;
oVars.email = oF.email.value;
for (var i=0,j=oF.sex.length; i<j; i++) {
if (oF.sex[i].checked) {
oVars.sex = oF.sex[i].value;
break;
}
}
oF.reset();
new AjaxReq("POST", oF.getAttribute("action"), oVars, myCallBack);
document.getElementById("msgDispl").firstChild.nodeValue = "Sender ...";
oVars = null;
}
</script>
</head>
<body>
<p id="msgDispl" style="font:bold 12px tahoma,sans-serif"> </p>
<form action="
http://www.domain.dk/sti/til/fil.php" onsubmit="sendAjaxReq(this);return false;">
<input type="hidden" name="context" value="insert">
<p>Navn: <input type="text" name="navn"></p>
<p>E-mail: <input type="text" name="email"></p>
<p>Køn: <input type="radio" name="sex" id="male" value="m" checked="checked"><label for="male">m</label>
<input type="radio" name="sex" id="female" value="k"><label for="female">k</label></p>
<p><button type="submit">Indsæt</button></p>
</form>
</body>
</html>
Så har du $_POST["context"], $_POST["navn"], $_POST["email"] og $_POST["sex"] på serveren. Hvad du skriver ud til 'fil.php' bliver sat ind i det første p-element - men det må kun være en tekststreng ... ikke HTML. Skal det HTML-formateres bliver det lidt mere indviklet, men ikke nødvendigvis meget :)