Oops, you're right. I was thinking of a normal form, where the PHP would control the response's layout.
As I thought about it, I realized that you actually need 2 copies of everything involved. Your script wasn't built for that, although it 'should' have been according to convention. I've rearranged it so it now works- with any number of forms and inputs,
- with minimal extra effort (compared to a normal form) on the designer's part, and
- in a degradable way, so browsers without JS will still submit the form as normal.
Note that your PHP needs to do something special for the last point to be true. I added an extra value, 'ajax=true', to the request string, so...
Code:
if(isset($_POST['ajax']) && $_POST['ajax'] == 'true'){
//Only output the data.
}else{
//Output an entire page.
}
Note also that it's made to accept GET forms as well, but support for that isn't implemented. But that would be really simple, mostly copy-and-paste.
Code:
<html>
<head>
<title>Titled Document</title>
<script type="text/javascript" language="javascript"><!--
function AjaxForm(form, display){
var that = this;
this.form = typeof form == 'string' ? document.forms[form] : form;
this.form.onsubmit = function (){
that[that.form.method.toLowerCase()]();
return false;
};
this.display = typeof form == 'string' ? document.getElementById(display) : display;
}
AjaxForm.prototype = {
request: null,
post: function(){
var that = this;
this.request = null;
var parameters = [];
for(var i = 0; i < this.form.elements.length; i++){
var element = this.form.elements[i];
parameters.push(element.name + '=' + element.value);
}
parameters.push('ajax=true');
if (window.XMLHttpRequest) { // Mozilla, Safari,...
this.request = new XMLHttpRequest();
if (this.request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
this.request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
this.request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
this.request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!this.request) {
alert('Cannot create XMLHTTP instance');
return false;
}
this.request.onreadystatechange = function () {
if (that.request.readyState == 4) {
if (that.request.status == 200) {
var result = that.request.responseText;
//alert(result);
that.display.innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
};
parameters = parameters.join('&');
this.request.open('POST', this.form.action, true);
this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.request.setRequestHeader("Content-length", parameters.length);
this.request.setRequestHeader("Connection", "close");
this.request.send(parameters);
}
};
window.onload = function(){
var form = new AjaxForm('addfriend', 'addfriend');
var form = new AjaxForm('addfavourite', 'addfavourite');
};
//--></script>
</head>
<body>
<form action="addFriend.php" method="POST" name="addfriend" id="addfriend">
<input type="hidden" name="userID" id="userID" value="{me.id}">
<input type="hidden" name="friendID" id="friendID" value="{user.id}">
<input type="submit" name="submit" value="Add to friends" class="ajaxbutton">
</form>
<form action="addFavourite.php" method="POST" name="addfavourite" id="addfavourite">
<input type="hidden" name="userID" id="userID" value="{me.id}">
<input type="hidden" name="friendID" id="friendID" value="{user.id}">
<input type="submit" name="submit" value="Add to favorites" class="ajaxbutton">
</form>
</body>
</html>
Bookmarks