Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 posts Page 1 of 1
Hello,

First part of the equation is submitting form in background. To do this, we need a hidden iframe and form will submit itself to that iframe (genius!):
Code: Select all
<cms:form method='post' name='my_form' anchor='0' target='transFrame'  >
   
    <cms:input type='submit' name='submit' value='Submit'/>
   
</cms:form>
<iframe style="display:none;" name="transFrame" id="transFrame"></iframe>


If we add 'action' parameter to the form, then it will load the url into the iframe, which can be used to request some other template. In my sample I just have the same template link.

target='transFrame' action="<cms:show k_template_link />" >


One sample is to change text in parent window's 'p' element after form submits:
Code: Select all
<p id="message"></p>    
<cms:form method='post' name='my_form' anchor='0' target='transFrame' action="<cms:show k_template_link />" >
   
    <cms:if k_success>
        <cms:abort><script>window.parent.document.getElementById('message').innerHTML = "New text!";</script></cms:abort>
    </cms:if>
    <cms:input type='submit' name='submit' value='Submit'/>
   
</cms:form>
<iframe style="display:none;" name="transFrame" id="transFrame"></iframe>


Another sample does the same, but controls the code flow, so we can abort executing the rest of the code of the template as soon as request is captured. In this case we don't need the whole code of the template to load in order to get to k_success block inside the form. I am adding a hidden input, which value can be intercepted somewhere in the beginning of the template, very similar to how we use "<cms:is_ajax />" tag.

Code: Select all
<cms:set faux_request = "<cms:gpc 'faux_request' />" />
<cms:if  faux_request >
    <cms:abort><script>window.parent.document.getElementById('message').innerHTML = "New text!";</script></cms:abort>
</cms:if>

<p id="message"></p>   
<cms:form method='post' name='my_form' anchor='0' target='transFrame' action="<cms:show k_template_link />" >
   
    <cms:input type='submit' name='submit' value='Submit'/>
    <cms:input type='hidden' name='faux_request' value='1' />
   
</cms:form>
<iframe style="display:none;" name="transFrame" id="transFrame"></iframe>


Possible application of this trick is the possibility to never use complicated Ajax javascript code and control response data (json or html) by Couch-code.

P.S. It won't hurt, of course, to use a function that calls a remote url ( here viewtopic.php?f=8&t=11368&p=30477#p30228 and here viewtopic.php?f=8&t=11368&p=30503#p30503) and combine that with approach above in certain cases.
SUPER, thank you !!
2 posts Page 1 of 1
cron