2

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:

  1. 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?
  2. Is there a better, more reliable way to control where the user ends up based on which submit button they've chosen?
2

I know it's a bit old but for someone looking to change some parameters (like the return value) on submit by JavaScript - the reason it's not possible is that EE uses the meta tag which is an encrypted string containing some of the information. So, to change the value you would need to recreate that encryption. Look in the file Channel_form_lib.php for more information.

| improve this answer | |
2

Matt, two options for you.

1) Easy: pass a segment variable to the safecracker form, then use 2 if statements (rather than an if else) to set the return values you need. That means youll need two different links pointing to the safecracker form.

2) More work: create an extension and use the safecracker hook - channel_form_submit_entry_end to redirect to the required page. safecracker hooks. Maybe you could use a hidden input value, pick that up in the post then redirect to it.

Edit: Solution 1: like this. on the page that links TO the safecracker form create 2 links.

href="template_group/template_name/submit"

href="template_group/template_name/submit-email"

And in your safecracker template...

{exp:safecracker
secure_action = "yes"
secure_return = "yes"
channel = "browse"
include_jquery = "no"
status = "{embed:status}"
{if segment_3 == "submit"}
return = "some/path"
{/if}
{if segment_3 != "submit"}
return = "another/path"
{/if}
}

| improve this answer | |
  • Also, with your current code, i just noted you have include_jquery="no". Are you including jquery yourself? – Lloyd Hill Aug 29 '13 at 10:22
  • Thanks, the extension route makes sense but I'm not following your #1 or seeing how that allows for the workflow I've described. Are you ultimately suggesting that I link to two separate forms, each with its own return value? – Matt Stein Aug 29 '13 at 15:42
  • One form. Answer updated with sample code. We use the segment variable to switch form into add or edit mode. Each has a different return value. You can use ths approach too. Its the quick way. – Lloyd Hill Aug 29 '13 at 17:37
  • Also, for this safecracker form it was an embed. So we could also have used an embed variable instead of a segment variable in the if statement. I try to only use one embed per template. – Lloyd Hill Aug 29 '13 at 17:45
  • Thanks for clarifying! I'm still missing something though: how does this approach allow the user's action (Submit A vs. Submit B) to determine the form's return value? – Matt Stein Aug 29 '13 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.