Skip to content

Instantly share code, notes, and snippets.

@zmsmith
Created September 14, 2010 14:48
Show Gist options
  • Save zmsmith/579149 to your computer and use it in GitHub Desktop.
Save zmsmith/579149 to your computer and use it in GitHub Desktop.
<div class="yourFancyBoxClass">
{% if success %}
<h2>It Worked!</h2>
{% else %}
<form action="{% url ajax_form %}">
{% form %}
</form>
{% endif %}
</div>
$(".yourLaunchClass").fancybox({onComplete:function (){
//grab this function so that we can pass it back to
//`onComplete` of the new fancybox we're going to create
var func = arguments.callee;
//bind the submit of our new form
$('.yourFancyBoxClass form').submit(function(){
//this is strictly cosmetic
$.fancybox.showActivity();
var data = $(this).serialize();
var url = $(this).attr('action')
//post to the server and when we get a response,
//draw a new fancybox, and run this function on completion
//so that we can bind the form and create a new fancybox on submit
$.post(url, data, function(msg){$.fancybox({content:msg,onComplete:func})});
return false;
});
}
});
def ajax_form(request):
context = {}
if request.method == "POST":
form = myForm(request.POST)
if form.is_valid():
form.save()
context['success'] = True
else:
form = WaitingListEntryForm()
context['form'] = form
return render_to_response("AjaxFancyBox.html", context, context_instance=RequestContext(request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment