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 04-22-2008, 03:37 AM   PM User | #1
zactanaz
New Coder

 
Join Date: Apr 2006
Location: Planet Earth
Posts: 59
Thanks: 1
Thanked 8 Times in 8 Posts
zactanaz is an unknown quantity at this point
AJAX POST does not update DIV tag

Hello,

Am trying to use AJAX to post a form, then update the DIV that the form is located in. But it does not update the DIV, it should display a message like "Thanks for posting" inside the DIV tag. It does post the data. Am not good at JavaScript so any help would be appreciated.

This is the CODE:

Code:
var http_request = false;
function makePOSTRequest(url, parameters, id) {
	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(id);
    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(id) {
	if (http_request.readyState == 4) {
    	if (http_request.status == 200) {
        //alert(http_request.responseText);
        	result = http_request.responseText;
            document.getElementById(id).innerHTML = result;            
        } else {
            alert('There was a problem with the request.');
        }
	}
}
   
function get(obj, id) {
	var poststr = "artist=" + encodeURI( document.getElementById("artist").value ) +
                  "&title=" + encodeURI( document.getElementById("title").value ) +
                  "&time=" + encodeURI( document.getElementById("time").value ) +
                  "&ytid=" + encodeURI( document.getElementById("ytid").value ) +
                  "&thumb=" + encodeURI( document.getElementById("thumb").value );
    makePOSTRequest('post.php', poststr, id);
}
And this is the HTML Form:
Code:
<div id="video_1">
	<form id="form_video_1" name="form_video_1" action="javascript:get(document.getElementById('form_video_1'), video_1);">
        Artist: <input id="artist" name="artist" type="text" size="20"><br>
    	Title: <input id="title" name="title" type="text" size="20"><br>
        <input id="time" name="time" type="hidden" value="123" />
        <input id="ytid" name="ytid" type="hidden" value="234124" />
        <input id="thumb" name="thumb" type="hidden" value="something" />
        <input name="submit" type="submit" value="Submit" onclick="javascript:get(this.parentNode);">
	</form>
</div>
I get the following error (in FireFox with FireBug):

Code:
video_1 is not defined
javascript:get(document.getElementById('form_video_1'), video_1);
Line 1
zactanaz is offline   Reply With Quote
Old 04-22-2008, 05:21 AM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
When you are doing this

http_request.onreadystatechange = alertContents(id);

you are telling alertContents to execute right than and there and store the result into onreadystatechange. What you need to do is store the function reference into it.

so you need to use a closure:

http_request.onreadystatechange = function(){alertContents(id);}

but since you are using a global variable for the xhr request, you can just use a global variable for the id.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Users who have thanked A1ien51 for this post:
zactanaz (04-22-2008)
Old 04-22-2008, 03:20 PM   PM User | #3
zactanaz
New Coder

 
Join Date: Apr 2006
Location: Planet Earth
Posts: 59
Thanks: 1
Thanked 8 Times in 8 Posts
zactanaz is an unknown quantity at this point
That was it, thank you.
zactanaz 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 06:55 AM.


Advertisement
Log in to turn off these ads.