 Phil. World Chat Champion

Joined: 02 Feb 2002 Karma :     
|
 Posted: 13:24 - 03 Apr 2009 Post subject: Prototype & Ajax Help |
 |
|
I picked up some javascript that works with the prototype frame work, i forget where it's from but i expect the changes i need to the code below are pretty simple, I just still dont have much of a clue with JS.
The purpose of this code is to pass form elements to a processing page then send back a confirmation or error message.
| Code: |
function sendRequest() {
new Ajax.Request("ajax.subscribe.php",
{
method: 'post',
postBody: 'list_email_add='+ $F('list_email_add'),
onComplete: showResponse
});
}
function showResponse(req){
$('morph').innerHTML= req.responseText;
}
|
It works fine at the moment, but i need to add a couple more fields to pass, what ever i try doesn't work. Any hints?
Update:
Code above isn't very flexible, but this code:
| Code: |
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
// <![CDATA[
document.observe('dom:loaded', function() {
function sendForm(event){
//stop the default submit behaviour
Event.stop(event);
var oOptions = {
method: "POST",
parameters: Form.serialize("ajaxForm"),
asynchronous: true,
onFailure: function (oXHR) {
$('feedback').update(oXHR.statusText);
},
onLoading: function (oXHR) {
$('feedback').update('<img src="images/loading.gif" alt="Loading" />');
},
onSuccess: function(oXHR) {
$('feedback').update(oXHR.responseText);
}};
var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "ajax.php", oOptions);
}
Event.observe('submitButton', 'click', sendForm, false);
});
// ]]>
</script>
<div id="feedback"> </div>
<div id="contact">
<form id="ajaxForm">
<input type="text" name="name" />
<input type="submit" id="submitButton" value="send contact form" />
</form>
</div>
|
Works pretty well, automatically passes any additonal form elements through post. Hope this comes in handy for someone, one day. |
|