Viewing 10 reply threads
  • Author
    Posts
    • #4928
      kattagami
      Member

      Hello,

      I’ve wondered if there is a way to build a contact form with Piklist 0.9.9.6?

      For a contact form, I don’t need to save the field datas in the database but just get them and then call the wp_mail() function.

      Is there any hooks to perform that?

      Thx

    • #4952
      Steve
      Keymaster

      @kattagami– You would use the Piklist hook piklist_save_field-{$scope}

      Here’s how to get started:
      1. Create your frontend form
      2. You still have to define the scope for each field, however, since you’re not saving the data choose something unique like contact. 'scope' => 'contact'
      3. In your main plugin file include this code:

        function my_contact_form_email($fields)
        {
          piklist::pre($fields);
          die();
        }
        add_action('piklist_save_field-contact','my_contact_form_email');
      

      When you save your form Piklist will trigger this function and you will see the array of field data that you have access to. Just loop through the array and create your mailer.

      Let us know if this works for you.

    • #4957
      kattagami
      Member

      Thanks very much for your help Steve, you always come with a solution 🙂
      It’s works like a charm! but …

      I use the “message” comment on top of my form php file to tell to my user that the message has been sent when they submit it.

      However, if a field doesn’t validate, the error message AND the “message” (which said everything is ok) both display above my form.

      How display the “message” only if there is no validate error messages?

      Thx
      Gilles

    • #4983
      Steve
      Keymaster

      This may be a bug. Looking into it.

    • #5143
      Stefan
      Member

      I’m creating travel booking website (piklist only) with exactly same issue. Hope this fix soon 🙂

    • #5267
      Marc
      Member

      Hi there,

      Just encountered the same problem.
      I looked around, and if in class-piklist-form.php line 3775 -> public static function notices
      you add && empty( Piklist_Validate::notices( self::$form_id, true) ) in the if statement it sorts the problem.

      Full code:

      
        public static function notices($form_id = null)
        {
          if (!empty(self::$form_submission)
              && self::$form_id
              && self::$form_id == $form_id
              && isset(self::$forms[self::$form_id])
              && !empty(self::$forms[self::$form_id]['data']['message'])
              && empty( Piklist_Validate::notices( self::$form_id, true) ) // Checks if there are any errors
            )
          {
            piklist::render('shared/notice', array(
              'id' => 'piklist_form_notice'
              ,'notice_type' => 'update'
              ,'dismiss' => is_admin()
              ,'content' => self::$forms[self::$form_id]['data']['message'] 
            ));
          }
        }
      

      At least it works for the frontend forms.
      Not sure if it would have any effect somewhere else!

    • #5269
      Marc
      Member

      I have an other question concerning the frontend form, and in that case a Contact form.
      I would like to change the content of the page if the submission is successful.

      For exemple

      
      if(success) :
        Notice: Thank you for Your message. Whatever...
        Content of your message:
         ...
         ...
      
        [button Click here to go back to the form]
      else :
        display form
      

      Is there a way to do that?
      I’ve found the elements necessary for that in the PiklistForm class, but the variables are private, so not accessible right now.

    • #5270
      Marc
      Member

      Well, I’ve found a way to do it.
      Here it is:
      (all the code is in the form’s file)

      
        $form_id = $_POST['_']['form_id']; 
        // if we don't have a form posted OR if the form posted contains errors: 
        if( !$form_id || !empty( Piklist_Validate::notices( $form_id, true) )  ) { 
          //display form
          piklist('field', array(
            'type' => 'text
            ,'scope' => 'contact'
            ...
          ));
          ....
        } else {
        ?>
        <h3>Your request:</h3>
        <?php 
      	foreach( $_POST['_contact'] as $label => $value ) {
      		echo '<p><strong>' . $label . ':</strong> ' . $value . '</p>';
      	}
      	?>
      	<a href="<?= the_permalink(); ?>" class="btn btn-default">Go back to Contact form</a>
      	<?php
        }
      
    • #5275
      Steve
      Keymaster

      @marcusing– Great job!

      Here’s a Pro Tip: Everything in Piklist is dynamic, including the underscore in the $_POST object. “_” is set as piklist::$prefix, but can be changed if you want to:

      So instead of:

      $form_id = $_POST['_']['form_id']; 
      

      Use this:

      $form_id = $_POST[piklist::$prefix]['form_id']; 
      

      Just keeps things future proof.

    • #5284
      Marc
      Member

      Thanks @Steve.
      I thought there was a more clever way to do 🙂
      So I suppose $_POST['_contact'] should be $_POST[ piklist::$prefix . 'contact' ] right?

    • #5286
      Steve
      Keymaster

      yup!

Viewing 10 reply threads
  • The topic ‘Build a Contact Form with Piklist 0.9.9.6?’ is closed to new replies.