Created
October 2, 2011 06:16
-
-
Save andrewminton/1257134 to your computer and use it in GitHub Desktop.
Ajax Submit form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ================== --> | |
<!-- = At Normal Page = --> | |
<!-- ================== --> | |
<xsl:template match="data"> | |
<h1>Contact Form</h1> | |
<form method="post" action="{$current-path}"> | |
<xsl:call-template name="send-message"/> | |
</form> | |
<script type="text/javascript"> | |
$('form').ajaxForm(); | |
$.fn.ajaxForm = function(){ | |
var self = $(this); | |
self.submit(function(e){ | |
e.preventDefault(); | |
$.ajax({ | |
type: self.attr('method'), | |
url: self.attr('action') + '/ajax/', | |
data: self.serialize(), | |
beforeSend: function(){ | |
$(':submit', self).attr({disabled: 'disabled'}); | |
}, | |
success: function(response){ | |
$('.ajax-wrapper').replaceWith($('.ajax-wrapper', response)); | |
} | |
}); | |
}); | |
return this; | |
} | |
</script> | |
</xsl:template> | |
<!-- ================ --> | |
<!-- = At Ajax Page = --> | |
<!-- ================ --> | |
<xsl:template match="data"> | |
<div> | |
<xsl:call-template name="send-message"/> | |
</div> | |
</xsl:template> | |
<!-- ========================= --> | |
<!-- = Send Message Template = --> | |
<!-- ========================= --> | |
<xsl:template name="send-message"> | |
<xsl:param name="event" select="/data/events/send-message"/> | |
<fieldset class="ajax-wrapper"> | |
<xsl:choose> | |
<xsl:when test="$event/@result = 'success'"> | |
<p class="success">Success!</p> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:if test="$event/@result = 'error'"> | |
<xsl:call-template name="errors-handler"> | |
<xsl:with-param name="event" select="$event"/> | |
</xsl:call-template> | |
</xsl:if> | |
<label> | |
Name | |
<input type="text" name="fields[name]" value="{$event/post-values/name}"/> | |
</label> | |
<label> | |
Message | |
<textarea name="fields[message]"><xsl:value-of select="$event/post-values/message" /></textarea> | |
</label> | |
<input type="hidden" name="action[send-message]"/> | |
<input type="submit" value="Submit"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</fieldset> | |
</xsl:template> | |
<xsl:template name="errors-handler"> | |
<xsl:param name="event" /> | |
<p class="error">Error!</p> | |
<!-- Display errors here --> | |
</xsl:template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment