We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8439
    • 89 Posts
    I’d like to construct an enquiry form where the client could tick a checkbox to indicate interest in one or more products, I imagine it looking something like this (html tags added to indicate final look and feel)

    [ ] <b>product1 code</b> Product1 details
    [ ] <b>product2 code</b> Product2 details
    .
    .
    .

    Other stuff, e.g. name, address

    Email would say something like person, address is interested in <list of products>

    The list of products will be pulled from a database.

    Ed
      • 30223
      • 1,010 Posts
      You can use the eformOnBeforeFormParse event for this. You could do something like this:

      1. Create a form template chunk with all fixed fields and a placeholder for the dynamically generated check boxes eg.

      
      <form method="post" action="[~[*id*]~]" id="enquiryForm">
      	<fieldset>
      		<h3> Contact Form</h3>
      		<label for="name">Your name: </label>
      		<input name="name" type="text" eform="Your Name::1:" />
      		<label for="email">Your Email Address:</label>
      		<input name="email"  type="text" eform="Email Address:email:1" />
      		<label for="products">Products</label>
      		[+productCheckBoxes+]
      		<label for="comments">Comments:</label>
      		<textarea name="comments" rows="4" cols="20" eform="Message:html:0"></textarea>
      
      		<input type="submit" name="contact" id="cfContact" class="button" value="Send This Message" />
      	</fieldset>
      </form>
      


      2.Create and save a separate snippet with the event function that will be used by eForm. This will need to fetches the product names from the database, build the checkboxes and replace the placeholder in the form template. Something like this:
      <?php
      //snippet 'eformEventsSnippet'  - makes function available to eForm
      function beforeFormParseFunction( &fields, &templates ){
          //some code for retrieving stuff from the database
          //....
          // let's assume the result is stored  in an array called $rows
      
          //build checkboxes
          foreach( $rows as $product )
              $p .= "<input type='checkbox' name='products[]' value='".$product['id']."'>".$product['name']."\n";
      
          //fill placeholder in form template
          str_replace('[+productCheckBoxes+]',$p,$templates['tpl']);
      
          return true; //important !
      }
      ?>


      3. Insert both the above snippet and eForm into your page.
      [[eformEventsSnippet]]
      [!eForm? &formid=`enquiryForm` &eFormOnBeforeFormParse=`beforeFormParseFunction` ...lots of other parameters... !]
      


      I hope you can work it our from here. eForm comes with some documentation and examples about it events. Have a read through that and the above might become a bit clearer. Hope this helps.