Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2008, 10:07 AM   PM User | #1
bslashdash
New to the CF scene

 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bslashdash is an unknown quantity at this point
Multiple AJAX POST Requests

Hi there, I was wondering if any of you guys on here can help me with this.

I'm a total newbie when It comes to AJAX and Javascript in general so forgive me if this is really simple.

I have a script that I found on www.captain.at which basically allows an html form to be submitted, processes the corresponding php file and shows the result.

The code is as follows

Code:
<script type="text/javascript" language="javascript">
			var http_request = false;
			function makePOSTRequest(url, parameters) {
			http_request = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
			}
			} else if (window.ActiveXObject) { // IE
			try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
			}
			}
			if (!http_request) {
			alert('Cannot create XMLHTTP instance');
			return false;
			}
			http_request.onreadystatechange = alertContents;
			http_request.open('POST', url, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", parameters.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(parameters);
			}
			function alertContents() {
			if (http_request.readyState == 4) {
			if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById('addfriend').innerHTML = result;
			} else {
			alert('There was a problem with the request.');
			}
			}
			}
			function get(obj) {
			var poststr = "userID=" + encodeURI( document.getElementById("userID").value ) +
			"&friendID=" + encodeURI( document.getElementById("friendID").value );
			makePOSTRequest('requests/addFriend.php', poststr);
			}
		</script>
And this is the code used for the form

Code:
<form action="javascript:get(document.getElementById('addfriend'));" name="addfriend" id="addfriend"> 
<input type="hidden" id="userID" value="{me.id}"> 
<input type="hidden" id="friendID" value="{user.id}"> 
<input type="button" name="button" value="Add to friends" onclick="javascript:get(this.parentNode);" class="ajaxbutton"> 
</form>
Now this works perfectly however I have more than one request I need to process in this way. The above code lets a user add another user to their friends list. I need the users to be able to add users to their favorites list also, and this would be done through a separate form.

What changes need to be made to the javascript code to allow me to have multiple forms leading to different php files on one page.

Any help would be greatly appreciated.

Thanks
bslashdash is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 AM.


Advertisement
Log in to turn off these ads.