I've inherited a project where the developer used a trick that's worked for me before: use JavaScript to modify the value of Safecracker's hidden return element prior to submitting the form, gaining control over where you'll land after the form is submitted.
Except it's not working with this EE 2.6.1 install; despite this working with EE 2.5.5, and the fact that I can easily modify the return element's value, the form submits and leaves me at the page specified in the Safecracker return parameter.
Here's what we'd like to happen on this school site:
- teacher composes a new class blog post
- teacher chooses "Submit" or "Submit and Email to Parents"
- if choosing "Email to Parents", resulting page should be
/mail_classblog_confirm/
, which loads up the last teacher blog post, previews it as an email, and has the teacher confirm that it should be sent out
What actually happens: either submission lands back at /classes/{freebie_2}
, which is specified in the {exp:safecracker}
return parameter.
<h3>New Post</h3>
{exp:safecracker channel="class_blog" return="/classes/{freebie_2}" include_jquery="no"}
<label>Title:</label>
<input type="text" name="title" style="width: 98%;">
<label>Update:</label>
{field:bl_content}
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-primary btn-mail">Submit and Mail to Parents</button>
<script>
$('.btn-mail').click(function(){
for ( instance in CKEDITOR.instances )
CKEDITOR.instances[instance].updateElement();
if ($('input[name="return"]').length > 0) {
$('input[name="return"]').val('/mail_classblog_confirm/');
} else {
$('form#publishForm div.hiddenFields').append("<input type='hidden' name='return' value='/mail_classblog_confirm/'>");
}
$('#publishForm').submit();
});
</script>
<a class="btn btn-cancel" style="float: right;" href="#">Cancel</a>
{/exp:safecracker}
Ultimately I have two questions:
- If I'm absolutely positive that the JavaScript is manipulating the return input element correctly (I can watch it happen in Chrome's inspector) prior to submitting the form, how is it even possible that this could fail?
- Is there a better, more reliable way to control where the user ends up based on which submit button they've chosen?