Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define( 'MAIL_TO', 'user@example.com' );
- define( 'MAIL_FROM', 'info@example.com' );
- define( 'SUBJECT', 'Website Feedback' );
- // Set default values and define what can be accepted via post
- $aryData = array_fill_keys( [ 'Name', 'Email', 'Discourse' ], '' );
- $aryData['Options'] = [];
- $aryErr = [];
- $bPosted = ( count( $_POST ) > 0 );
- $bValidated = false;
- function clean_string( $string, $bad = [ 'content-type', 'bcc:', 'to:', 'cc:', 'href' ] ) {
- return str_replace( $bad, '', $string );
- }
- if ( $bPosted ) {
- // Move POST values into main Data array
- foreach( $_POST as $key => $val ) {
- if ( preg_match( '/^(txt|drp|hid|chk|rad)([a-zA-Z0-9_-]+)$/', $key, $regs ) && array_key_exists( $regs[2], $aryData ) ) {
- $aryData[ $regs[2] ] = ( is_string( $val ) ) ? trim( $val ) : $val;
- }
- }
- // Validate Information
- if ( $aryData['Name'] == '' ) {
- $aryErr['Name'] = 'Name is required';
- }
- elseif ( !preg_match( '/^[a-z .\'-]+$/i', $aryData['Name'] ) ) {
- $aryErr['Name'] = 'Name contains invalid characters';
- }
- if ( $aryData['Email'] == '' ) {
- $aryErr['Email'] = 'Email is required';
- }
- elseif ( !filter_var( $aryData['Email'], FILTER_VALIDATE_EMAIL ) ) {
- $aryErr['Email'] = 'Invalid Email address';
- }
- if ( $aryData['Discourse'] == '' ) {
- $aryErr['Discourse'] = 'Please provide some comments';
- }
- $bValidated = ( count( $aryErr ) == 0 );
- }
- if ( $bPosted && $bValidated ) {
- $aryMsg = [];
- $aryMsg[] = 'Form details below:';
- $aryMsg[] = "\n";
- $aryMsg[] = 'Name: ' . clean_string( $aryData['Name' ] );
- $aryMsg[] = 'Email: ' . clean_string( $aryData['Email'] );
- $aryMsg[] = 'Comments: ' .clean_string( $aryData['Discourse'] );
- $aryMsg[] = 'Options: ';
- if ( count( $aryData['Options'] ) == 0 ) {
- $aryMsg[] = ' (Non Selected)';
- }
- else {
- foreach( $aryData['Options'] as $val ) {
- $aryMsg[] = ' ' . $val;
- }
- }
- $strHeaders = 'From: ' . MAIL_FROM . "\r\n" .
- 'Reply-To: ' . MAIL_FROM . "\r\n" .
- 'X-Mailer: PHP/' . phpversion();
- // Commented out for testing...
- // if ( !mail( MAIL_TO, SUBJECT, implode( "\n", $aryMsg ), $strHeaders ) ) {
- // die( 'There was an unexpected error sending contact form' );
- // }
- // For Debug Only
- echo '<div style="white-space: pre; border: #000 1px solid; background-color: #ccc; font-family: monospace; padding: 20px;">';
- echo 'To: ' . MAIL_TO . "\n" . $strHeaders ;
- echo '<hr>';
- echo implode( "\n", $aryMsg );
- echo '</div>';
- }
- function echoTextInput( &$data, $field, $label = '', $placeholder = '', $extra = '' ) {
- if ( $label == '' ) { $label = $field; }
- if ( $placeholder == '' ) { $placeholder = $label; }
- echo '<label for="txt', $field, '">', htmlspecialchars( $label, ENT_QUOTES ), "</label>\n";
- echo '<input name="txt', $field, '" id="txt', $field, '" class="txtinput" ';
- echo 'placeholder="', htmlspecialchars( $placeholder, ENT_QUOTES ), '" ';
- echo 'value ="' . htmlspecialchars( $data[ $field ], ENT_QUOTES ), '" ', $extra;
- if ( strpos( $extra, 'type=' ) === false ) {
- echo ' type="text" ';
- }
- echo " >\n";
- }
- function echoCheckboxInput( &$data, $field, $value = '', $label = '', $extra = '' ) {
- static $ids = [];
- if ( !array_key_exists( $field, $ids ) ) { $ids[ $field ] = 0; }
- $ids[ $field ]++;
- if ( $value == '' ) { $value = $ids[ $field ]; }
- if ( $label == '' ) { $label = $field; }
- echo "<div>\n";
- echo '<label for="chk', $field, '-', $ids[ $field ], '" class="tag">', htmlspecialchars( $label, ENT_QUOTES ), "</label>\n";
- echo '<input type="checkbox" id="chk', $field, '-', $ids[ $field ], '" ';
- echo 'name="chk', $field, '[]" class="regular-checkbox big-checkbox" ';
- echo 'value="', htmlspecialchars( $value, ENT_QUOTES ), '" ';
- if ( in_array( $value, $data[ $field ] ) ) {
- echo ' checked="checked" ';
- }
- echo ">\n<div>\n";
- }
- ?><html>
- <head><title>Contact</title></head>
- <body>
- <h2>Contact</h2>
- <?php if ( $bPosted && $bValidated ) { ?>
- <p>Thank you for contacting us!</p>
- <?php } else { ?>
- <?php if ( $bPosted && !$bValidated ) { ?>
- <div class="errors">
- Check the following and try again:
- <ul><li><?php echo implode( '</li><li>', $aryErr ); ?></li></ul>
- </div>
- <?php } ?>
- <section class="contact">
- <form id="contact" action="" method="post">
- <fieldset title="About you">
- <legend><span>About you</span></legend>
- <?php echoTextInput( $aryData, 'Name', 'Name', 'Your Name', 'tabindex="10" required' ); ?>
- <?php echoTextInput( $aryData, 'Email', 'Email', 'Valid Email', 'tabindex="20" required type="email"' ); ?>
- </fieldset>
- <fieldset title="Your turn">
- <legend><span>Your turn</span></legend>
- <p>What, exactly then, are you after?</p>
- <?php echoCheckboxInput( $aryData, 'Options', 'salvation', 'Salvation' ); ?>
- <?php echoCheckboxInput( $aryData, 'Options', 'question', 'Question' ); ?>
- <?php echoCheckboxInput( $aryData, 'Options', 'other', 'Other' ); ?>
- <label for="txtDiscourse">Write your comments here</label>
- <textarea id="txtDiscourse" name="txtDiscourse" tabindex="30" class="txtinput" rows="7" placeholder="This is where your thoughts go"><?php echo htmlspecialchars( $aryData['Discourse'], ENT_QUOTES ); ?></textarea>
- <section id="buttons">
- <input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset">
- <input type="submit" name="submit" id="submitbtn" class="submitbtn" tabindex="40" value="Send away!">
- <br style="clear:both;">
- </section>
- </fieldset>
- </form>
- </section>
- <?php } // END: else: if( Posted and Valid ) ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement