Problems, need help? Have a tip or advice? Post it here.
13 posts Page 1 of 2
Hello,

I am trying to add a field to a page, a simple checkbox.
If that checkbox is checked, and that I click the SAVE button, nothing should happen. But if that checkbox isn't checked, I need it to send an email when I click on the "SAVE" button, and that it checks that checkbox right after, so that the next time I click SAVE again, it won't trigger the "send_email" function since the checkbox would now be checked.

Simple conditions, but I'm having hard time finding out how to do it.

Right now I have this code in the custom theme code for that page :

Code: Select all
    <cms:if k_success >
            <cms:pages id=k_last_insert_id limit='1' show_future_entries='1'>
                        <cms:if checkbox_checked!='Checked'>
            <cms:send_mail from='admin@mysite.com' to="<cms:show conferenciers3/>" debug="1">
        EMAIL CONTENT

        </cms:send_mail>
        <cms:db_persist_form
                _invalidate_cache='1'
                _token=k_cur_token
                checkbox_checked = 'Checked'
            />
</cms:if>
    </cms:if>
        </cms:pages>

             </cms:if>


And here's the code for that simple checkbox field on the main page file :

Code: Select all
   <cms:editable name='checkbox_checked' label="Cancel sent?" type='checkbox' opt_values="Checked" order="0"/>


Any idea what I should do?
Thanks a lot
Hi,

I think the following should help -
Code: Select all
<cms:form  ..    >
    <cms:if k_success>
   
        <cms:if frm_checkbox_checked ne 'Checked'>
            <cms:set frm_checkbox_checked = 'Checked' />
            <cms:set my_send_mail = '1' />
        </cms:if>
       
        <cms:db_persist_form
            _invalidate_cache='1'
            _token=k_cur_token
            checkbox_checked = frm_checkbox_checked
        />
       
        <cms:if k_success>
            <cms:if my_send_mail >
                .. send mail here ..
            </cms:if>

        </cms:if>
    </cms:if>

    <!-- form goes here -->
    <cms:input type='bound' name='checkbox_checked' />

</cms:form>

Please try it out and let me know.
Hi KK,

Thanks, but with that code, when I click on SAVE, it correctly checks that checkbox, send an email, but if I click save again, with the checkbox now checked, it will still send an email.

At first, the checkbox will be unchecked, and when I click SAVE, it needs to become checked and sends an email.
Once the email is sent and that the checkbox is checked though, clicking SAVE shouldn't send anything anymore.

Here's my full "cms:form" code:
Any idea what's wrong?

Code: Select all
<cms:form
    masterpage = k_selected_masterpage
    mode = k_selected_form_mode
    page_id = k_selected_page_id
    enctype = 'multipart/form-data'
    method = 'post'
    anchor = '0'
    add_security_token = '0'
    id = k_cur_form
    name = k_cur_form
    token = k_cur_token
>

    <div class="tab-pane fade active in" id="tab-pane-<cms:show k_route_module />">

        <cms:if k_success >

        <!-- Update variable if not checked -->
        <cms:if frm_checkbox_checked ne 'Checked'>
            <cms:set frm_checkbox_checked = 'Checked' />
            <cms:set my_send_mail = '1' />
        </cms:if>
        <!--  -->

            <cms:db_persist_form
                _invalidate_cache='1'
                _token=k_cur_token
                checkbox_checked = frm_checkbox_checked
            />

            <cms:if k_success >

            <!-- Send email if my_send_mail is equal to 1 -->
            <cms:if my_send_mail >
                <cms:send_mail from='admin@mysite.com' to="test@test.com" debug="1">
                    Sorry, pas accepté.
                </cms:send_mail>
            </cms:if>
            <!--  -->

                <cms:if k_redirect_link >
                    <cms:set_flash name='submit_success' value='1' />
                    <cms:redirect k_redirect_link />
                </cms:if>
            </cms:if>
        </cms:if>

        <cms:if k_error >
            <cms:show_error>
                <cms:each k_error >
                    <cms:show item /><br>
                </cms:each>
            </cms:show_error>
        </cms:if>

        <!-- advance settings dropdown -->
        <cms:render 'group_advanced_settings' />

        <!-- the editable regions -->
        <cms:admin_form_fields depth='1'>
            <cms:render 'form_row' />
        </cms:admin_form_fields>

        <div class="ctrl-bot">
            <cms:render 'page_actions' />
            <cms:render 'extended_actions' />

            <button class="btn tt" id="top" title="Scroll to Top" type="button"><cms:show_icon 'arrow-thick-top' /></button>
        </div>

        <input type="hidden" id="k_custom_action" name="k_custom_action" value="">
    </div>
    <cms:input type='bound' name='checkbox_checked' />
</cms:form>
I'll give you some tips on trying to debug the problem -

The crucial part of our code is the following where we check if 'frm_checkbox_checked' is not checked and set the flag for sending the mail -
Code: Select all
<cms:if frm_checkbox_checked ne 'Checked'>

Once the checkbox is checked, our assumption is that 'frm_checkbox_checked' should be equal to 'Checked' and thus the flag for mail will not be set.

Since this is not happening, you should examine the variables at the point where we use them to see why our assumption is going wrong.

For that place the following just above the <cms:if frm_checkbox_checked ne 'Checked'> statement -
Code: Select all
<cms:abort>
    <cms:dump_all />
</cms:abort>

Submit the form with the checkbox in checked state. You should see a list of all variables available at the point we are interested in. See what the value of 'frm_checkbox_checked' variable is. Is it 'Checked'?

That should help you in fixing the issue.

Hope this helps.
Hi KK,

Thanks.
Yes, it is indeed checked, based on the dump.

I tried changing that part too to see if the my_send_mail variable was working properly :

Code: Select all
<cms:if my_send_mail != '1' >
                <cms:send_mail from='admin@mysite.com' to="test@test.com" debug="1">
                    text
                </cms:send_mail>
            </cms:if>


And it's not sending any emails.
If I change it back to
Code: Select all
<cms:if my_send_mail = '1' >

or
Code: Select all
<cms:if my_send_mail >


It's working again.

Any ideas what's wrong?
Another thing I forgot to mention, is that the checkbox should be checked only if another field is filled too.

The first is a dropdown menu, so the condition should be more something like this :

Code: Select all
<cms:if (my_send_mail) && (conferencier_select)>


Here's how the whole page should work :

- The user creates a proposal.

- On this proposal, there's a repeatable region where he can enter as many names as he wants. The names are related to some workers name. Who might be the best fit for the clients request on the proposal.

- Once the client receives the link to the proposal newly created, and decides which worker he wants to hire. That client contacts the person who handles this part of the process, and then, in Couch admin, on that Proposal page, he must select the name of the worker who has been selected by the client from inside a dropdown menu.

- Once the "official" worker has been selected in the dropdown menu, and that the user in the admin panel clicks "SAVE", it must send an email to every other workers on were on that proposal, to tell them "Sorry you have not been selected on this proposal". The one in that dropdown menu though must receive an email saying "Congrats, you have been selected".

- Once these emails have been sent once, it shouldn't be fired everytime the user clicks on "SAVE" for that proposal. Only once, and it only happens when the "official" name has been selected in that dropdown field, and that the user clicks SAVE. If the "official name" dropdown is empty, nothing should happen when the user clicks SAVE. Once the user selects a name and click SAVE though, it shoots an email to a list of emails, and shouldn't happen again.

Sorry if that's confusing, it's a pretty simple concept, I'm just having a hard time explaining it.
Tell me if that makes sense.
For now let us stick to only the first problem.

From your last mail it seems you are sending the emails in a loop and I suspect that is causing the 'my_send_mail' variable to retain the '1' value once the first mail is sent and is never reset to '0'.

Please modify the following portion of the code -
Code: Select all
<cms:if frm_checkbox_checked ne 'Checked'>
    <cms:set frm_checkbox_checked = 'Checked' />
    <cms:set my_send_mail = '1' />
</cms:if>

- to make it as follows
Code: Select all
<cms:if frm_checkbox_checked ne 'Checked'>
    <cms:set frm_checkbox_checked = 'Checked' />
    <cms:set my_send_mail = '1' />
<cms:else />
    <cms:set my_send_mail = '0' />
</cms:if>

Does that make a difference?
Thanks KK.

Now I see that if I uncheck the checkbox, and save the page, it will not trigger the send_mail function.

So all that remains, is to make sure that it sends that email once.

What I thought of is :

At first, the checkbox is unchecked.

If the other required dropdown menu has an option selected, and that the checkbox is uncheck, I click SAVE and it sends the email and check that checkbox. But the checkbox would not change to "checked" if that dropdown menu has no option selected yet. Until I select an option in the dropdown menu, nothing should change and that checkbox should remain unchecked.

Then, if the user goes back to that page, and click SAVE, since the checkbox is checked, it would not send any emails when clicking SAVE.

Thanks! I think we're getting close to finding the solution here
Sorry to bump this one too. Like I said in my other topic, this issue is related to one of the biggest project I've made yet with Couch, and I need to send the data to my client this. week.

Thanks a lot for understanding, and thanks for you help
I am actually having a hard time wrapping my head around your requirement.

To make it easier for me to understand it, please either post some screenshots or wireframes illustrating the steps or, if your site is online, allow me to take a look at it.
13 posts Page 1 of 2