...

View Full Version : jQuery: XML data, post(), and each()



lauthiamkok
06-28-2009, 04:00 AM
hi,
this is the xml data being processed from 'comment_add_xml.php',

<response>
<error>comment_email</error>
<error>comment_content</error>
<result>Please leave an email.</result>
<result>Please leave a content.</result>
</response>

this is the jquery code supposed to post data from a html form below, it post the data from the form to 'comment_add_xml.php', then grab the xml data from it, and then display the data on the form again,


this.commentAdd = function(){
$("#result_comment").html('<div class="ajaxloader"><p><img src="img_iconies/loader_0.gif"/> loading...</p></div>');
$.post("comment_add_xml.php",{
cmt_email:$('#cmt_email').val(),
cmt_content:$('#cmt_content').val(),
pg_id:$('#pg_id').val()
},function(xml){
addMessages(xml);
});
return false;
}

function addMessages(xml) {
$("error",xml).each(function(i) {
$("#"+$("error",xml).text()+"_label").addClass('error');
/*
temp = $("error",xml).text();
alert(temp);
*/
});
}

addMessages(xml) is not finished yet bcos above it should 'break' each error and result into individual elements, but instead, it joins them up together like this,

error=> comment_emailcomment_content

result=> Please leave an email.Please leave a content.

Am I using the correct jquery function to process the xml data above - each()?? how can I break them into individual elements??

this is html form,

<form action="#" onsubmit="commentAdd(); return false;" method="post" enctype="multipart/form-data" id="form_comment">

<div class="form_item">
<label class="title" id="comment_email_label">
<input id="cmt_email"/>&laquo; Email
</label><img src="img_iconies/attention.png" style="visibility:hidden;" id="comment_email_img"/>
</div>

<div class="form_item">
<label class="title" id="comment_content_label">
<textarea id="cmt_content"></textarea>
</label><span style="visibility:hidden;" id="comment_content_img"> </span>
</div>

<div class="form_item">

<div class="form_left">
<input type="submit" name="update" value="Submit" class="button button_submit "/>
<input id="pg_id" name="pg_id" type="hidden" value="<?php echo $_REQUEST['pg_id'];?>" />
</div>

<div id="result_comment" class="form_right"></div>
</div>
</form>

please let me know if u have any ideas.

many thanks,
Lau



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum