Save / Output HTML form

Discussion in 'HTML & Website Design' started by ultramel123, Apr 25, 2013.

  1. #1
    I have a custom html calculating form in WordPress. I would like to know whether I can create a button, which first prints /saves the form to PDF, and then email's it to the relevant staff (and also including the person who completed the form)

    I have this so far, which only sends an email to the address specified in the contact form, and not to a default address as well.


    <?php
    /*
    Template Name: Contact
    */
    ?>
     
    <?php
    if(isset($_POST['submitted'])) {
        if(trim($_POST['contactName']) === '') {
            $nameError = 'Please enter your name.';
            $hasError = true;
        } else {
            $name = trim($_POST['contactName']);
        }
     
        if(trim($_POST['email']) === '')  {
            $emailError = 'Please enter your email address.';
            $hasError = true;
        } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
            $emailError = 'You entered an invalid email address.';
            $hasError = true;
        } else {
            $email = trim($_POST['email']);
        }
     
        if(trim($_POST['comments']) === '') {
            $commentError = 'Please enter a message.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $comments = stripslashes(trim($_POST['comments']));
            } else {
                $comments = trim($_POST['comments']);
            }
        }
     
        if(!isset($hasError)) {
            $emailTo = get_option('tz_email');
            if (!isset($emailTo) || ($emailTo == '') ){
                $emailTo = get_option('admin_email');
            }
            $subject = '[PHP Snippets] From '.$name;
            $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
            $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
     
            wp_mail($emailTo, $subject, $body, $headers);
            $emailSent = true;
        }
     
    } ?>
     
    <?php get_header() ?>
     
        <div id="container">
            <div id="content">
                <?php the_post() ?>
                <div id="post-<?php the_ID() ?>" class="post">
                    <div class="entry-content">
    <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
        <ul>
            <li>
                <label for="contactName">Name:</label>
                <input type="text" name="contactName" id="contactName" value="" />
            </li>
            <li>
                <label for="email">Email</label>
                <input type="text" name="email" id="email" value="" />
            </li>
            <li>
                <label for="commentsText">Message:</label>
                <textarea name="comments" id="commentsText" rows="20" cols="30"></textarea>
            </li>
            <li>
                <button type="submit">Send email</button>
            </li>
        </ul>
        <input type="hidden" name="submitted" id="submitted" value="true" />
    </form>
     
                    </div><!-- .entry-content ->
                </div><!-- .post-->
            </div><!-- #content -->
        </div><!-- #container -->
     
     
    <?php get_footer() ?>
    Code (markup):

     
    ultramel123, Apr 25, 2013 IP
  2. SmallBizWebsites.Org

    SmallBizWebsites.Org Greenhorn

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    5
    #2
    Go to the following URL, which is an excellent resource, for an example of how to use PHP to create a PDF file. You can then attach the file to an email you send using the PHP mail function.

    http://www.sitepoint.com/generate-pdfs-php/
     
    SmallBizWebsites.Org, Apr 25, 2013 IP
  3. ultramel123

    ultramel123 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thank you. I have installed the library, and it is finding it.
    It starts creating the pdf, but then an error pops up saying "file does not begin with '%PDF' ?

    What could be causing this error?
     
    Last edited: Apr 26, 2013
    ultramel123, Apr 25, 2013 IP