Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
Hello all!

I was hoping one of you clever folks would be able to help.

I'm actually pretty new to all aspects of development (this is about week 6), although I am managing to get to grips with most of the fundamentals, there are a few things that still throw me.

I've successfully managed to build a site and integrated Couch (which is awesome) however there is something I really just can't figure out how to do.

I've built a typical contact form and used Couch to 'process' it, however after submission of the form (which works fine and successfully shows a 'success' message) the fields of the form remain populated with the users input.

Is there a simple way to reset the form back to blank after submission?

I've included my forms code below, any help would be greatly appreciated - I know its a big ask, but if you could try and air on the simpler side it would be appreciated too.

Code: Select all
     <cms:form action='' method='post' id='contact_form'>

      <cms:if k_success >
      <p id='mail_success' class='success' style='display:block'>Thank you! Your message has been received. Our support team will get back to you as soon as possible.</p>
      
      <cms:send_mail from=k_email_from to=k_email_to subject='Contact Form Message'>
<strong>The following message has been sent for your attention: </strong>
         <cms:show k_success />
         </cms:send_mail>
      </cms:if>         

      <label>Full Name</label>
        <cms:input type="text" name='name' id='name' required='1' placeholder="John Smith" />
      <cms:if k_error_name>
         <p id='name_error' class='error' style='display:block'><i class="fi-prohibited"></i>  PLEASE ENTER YOUR NAME</p>
      </cms:if>
      <label>Email</label>
        <cms:input type="text" name='email' id='email' required='1' validator='email' placeholder="name&#64;example.com" />
      <cms:if k_error_email>
      <p id='email_error' class='error' style='display:block'><i class="fi-prohibited"></i>  PLEASE ENTER A VALID EMAIL ADDRESS</p>
      </cms:if>
      <label>Your Message</label>
      <cms:input type='textarea' name='message' id='message' required='1' rows="10" cols="50" placeholder="Type your message here."></cms:input>
      <cms:if k_error_message>
      <p id='message_error' class='error' style='display:block'><i class="fi-prohibited"></i>  PLEASE ENTER A MESSAGE</p>
      </cms:if>

      <input type="submit" class="small button large-push-10 small-push-6" value="Send Message" />

    </cms:form>


Thanks again!

Ash
Hello and welcome to our forums, Ash :)

There can be several ways of doing what you mentioned (have been discussed in many threads) but following is my favorite -

Upon successful submission of the form, redirect the visitor to the same page.
Since the page is loaded afresh, all the inputs appear in their pristine state.

Of course, this begs the question of how to show any success message (as your code is doing). For that, just before redirecting we save the success message in a 'session variable' (viewtopic.php?f=5&t=7377)

'Session variables' remain available for only one page reload, so when the page gets reloaded, the success message is shown.

I'll describe how you can use this technique.
1. Please open 'couch/addons/kfunctions.php' (if instead of this file you find one named 'kfunctions.example.php', please rename it to 'kfunctions.php') in a text editor.

2. Remove the '//' (i.e. uncomment) the following line -
Code: Select all
//require_once( K_COUCH_DIR.'addons/cart/session.php' ); 


3. The above two steps will make 'session variables' available for our use.
Finally, modify your form code to this (I am omitting some existing code for clarity )-
Code: Select all
<cms:if "<cms:get_flash 'flash_msg' />" >
    <p id='mail_success' class='success' style='display:block'>Thank you! Your message has been received. Our support team will get back to you as soon as possible.</p>
</cms:if>

<cms:form method="post" anchor='0'>
   <cms:if k_success >
        <cms:send_mail from=k_email_from to=k_email_to subject='Contact Form Message'>
            <strong>The following message has been sent for your attention: </strong>
            <cms:show k_success />
        </cms:send_mail>
       
        <cms:set_flash name='flash_msg' value="1" />
        <cms:redirect k_page_link />   
   </cms:if>
   
   ..
   ..

</cms:form>

Notice how immediately after you send the mail, we set a session variable named 'flash_msg' and redirect.
The redirected (i.e. reloaded) page finds the session variable and displays the success message.
The inputs appear blank.

Hope this helps.
Hello!

Thanks for the amazingly fast response, I had taken a look through a few alternative methods and tried fighting with a bit of javascript without much success.

Your method was awesome, it's done exactly what I wanted and only took me a couple of minutes to figure out.

Thanks so much for the help!

Ash
You are welcome, Ash :)
I am glad it helped.
4 posts Page 1 of 1
cron