Jump to content

giannisok

Members
  • Posts

    24
  • Joined

  • Last visited

Everything posted by giannisok

  1. Thanks for sharing Daniels! It would be a good idea to make it backwards compatible with PHP 5.5 so that it works on older installations otherwise mention that it has PHP 7 dependency. Regards!
  2. I am sending the NEW version of Simple Contact Form. This new version uses module MarkupGoogleRecaptcha in order to render the captcha. If you have downloaded Valitron validator through composer then add the following line to your _head.php: include(dirname(__FILE__) . "/../../vendor/autoload.php"); or include the old-way on the top of "partials/contact/_controller.php": require(dirname(__FILE__) . "/../../vendor/vlucas/valitron/src/Valitron/Validator.php"); There are 3 files currently: contact.php (template) partials/contact/_controller.php partials/contact/_email.php First file contact.php: <?php namespace ProcessWire; include('partials/contact/_controller.php'); ?> <h2><?php echo __('Contact Form') ?></h2> <?php if($session->flashMessage):?> <div class="alert <?php echo $session->sent ? 'alert-success' : 'alert-danger'?>" role="alert"> <?php echo $session->flashMessage;?> </div> <?php endif;?> <form id="contact-form" action="<?php echo $page->url;?>" method="post"> <div class="form-group <?php echo $v->errors('name') ? 'has-error' : ''?>"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-user"></i></span> <input required class="input-lg form-control" name="name" id="name" type="text" value="<?php echo $name?>" placeholder="<?php echo __('Name') ?>"> </div> </div> <div class="form-group <?php echo $v->errors('email') ? 'has-error' : ''?>"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-envelope"></i></span> <input required class="input-lg form-control" name="email" id="email" type="email" value="<?php echo $email?>" placeholder="<?php echo __('Email') ?>"> </div> </div> <div class="form-group <?php echo $v->errors('phone') ? 'has-error' : ''?>"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-phone"></i></span> <input class="input-lg form-control" name="phone" id="phone" type="tel" value="<?php echo $phone?>" placeholder="<?php echo __('Phone') ?>"> </div> <div class="field-notice" rel="phone"></div> </div> <div class="form-group <?php echo $v->errors('message') ? 'has-error' : ''?>"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa fa-quote-left"></i></span> <textarea rows="7" class="input-lg form-control" placeholder="<?php echo __('Message') ?>" name="message" id="message"><?php echo $message?></textarea> </div> </div> <div class="form-group"> <?php echo $captcha->render()?> </div> <button id="contact_send" class="btn btn-default" type="submit"><i class="fa fa-paper-plane" aria-hidden="true"></i> <?php echo __('SEND') ?></button> </form> <?php $session->remove('flashMessage'); $session->sent = false; echo $captcha->getScript(); ?> Then file partials/contact/_controller.php: <?php namespace ProcessWire; use Valitron\Validator; $captcha = $modules->get("MarkupGoogleRecaptcha"); $name = $sanitizer->text($input->post->name); $email = $sanitizer->email($input->post->email); $phone = $sanitizer->text($input->post->phone); $message = $sanitizer->text($input->post->message); $v = new Validator([ 'name' => $name, 'email' => $email, 'message' => $message, ]); $v->rule('required', ['name', 'email', 'message']); $v->rule('email', 'email'); $contactFormRecipient = 'your@company.com'; if ($input->post->name) { if ($v->validate()) { if ($captcha->verifyResponse() === true) { $subject = 'Contact Form'; $messageHTML = include_once('partials/contact/_email.php'); $mail = wireMail() ->to($contactFormRecipient) ->header('Reply-To', $email) ->subject($subject) ->bodyHTML($messageHTML); if ($mail->send()) { $session->flashMessage = __('Thank you for your message! We will get back to you.'); $session->sent = true; $session->redirect($page->url); } else { $session->flashMessage = __('Mail not sent. Error occured.'); } } else { $session->flashMessage = __('Recaptcha Validation Error'); } } else { $session->flashMessage = __('Please correct the errors and try again.'); } } ?> And finally partials/contact/_email.php: <?php ob_start();?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $subject; ?></title> </head> <body> <h1>New message from <i><?php echo $name; ?></i></h1> <p><?php echo $message; ?></p> <hr> <h3>Contact Info:</h3> <p><strong>Name:</strong> <?php echo $name; ?></p> <?php if(!empty($phone)):?> <p><strong>Phone:</strong> <?php echo $phone; ?></p> <?php endif;?> <p><strong>Email:</strong> <a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a></p> </body> </html> <?php return ob_get_clean();?> Hope you like it!
  3. Thanx @clsource ! I will as soon as i manage to publish it first! Regarding updates i am not thinking of providing any special update strategy. New versions will have the latest database & files and list with all the files that have changed. See ya
  4. Thanks @kongondo ! I hope also!
  5. I also think that Themeforest / Codecanyon can potentially be a great place for making Processwire more popular.
  6. Hello guys! i'm here to solve the mystery of seavuel.com ! I'm the owner. I am planning to release a commercial site-profile for a hotel-website on codecanyon.net What do you think about the idea/project? I mostly think of it as a long-term project with regular updates and a low-price.
  7. Replace include with require: require(dirname(__FILE__) . "yourfolder/vlucas/valitron/src/Valitron/Validator.php"); require(dirname(__FILE__) . 'yourfolder/google/recaptcha/src/ReCaptcha/ReCaptcha.php'); This way if the paths are not correct you will get an error...
  8. Yes it is... Instead of using composer just download the 2 libraries: Google recaptcha for validating the user is not a robot (https://github.com/google/recaptcha) Valitron validation library for validating the form fields (https://github.com/vlucas/valitron) unzip them inside a folder ...and replace the lines: include(dirname(__FILE__) . "/../../vendor/vlucas/valitron/src/Valitron/Validator.php"); include(dirname(__FILE__) . '/../../vendor/google/recaptcha/src/ReCaptcha/ReCaptcha.php'); with: include(dirname(__FILE__) . "yourfolder/vlucas/valitron/src/Valitron/Validator.php"); include(dirname(__FILE__) . 'yourfolder/google/recaptcha/src/ReCaptcha/ReCaptcha.php'); Hope it helps! Actually composer doesn't do any magic...it just downloads the libraries inside the vendor folder but you can always do it manually if you want.
  9. And here is the ALL-IN-ONE-FILE-WITHOUT-COMMENTS version...just put this in your contact.php template file (don't forget about composer update though): <?php namespace ProcessWire; include(dirname(__FILE__) . "/../../vendor/vlucas/valitron/src/Valitron/Validator.php"); include(dirname(__FILE__) . '/../../vendor/google/recaptcha/src/ReCaptcha/ReCaptcha.php'); $v = new \Valitron\Validator(array( 'name' => $sanitizer->text($input->post->name), 'email' => $sanitizer->email($input->post->email), 'message' => $sanitizer->text($input->post->message), ) ); $v->rule('required', ['name', 'email', 'message']); $v->rule('lengthMin', 'name', 5); $v->rule('email', 'email'); $googleSiteKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $googleSecretKey = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'; $contactFormRecipient = 'your@company.com'; $contactPageID = '1045'; if ($input->post->name) { if ($v->validate()) { $reCaptcha = new \ReCaptcha\ReCaptcha($googleSecretKey); $resp = $reCaptcha->verify($input->post->{'g-recaptcha-response'}, $_SERVER["REMOTE_ADDR"]); if ($resp->isSuccess()) { $message = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Contact Form | ' . $input->post->name . '</title> </head> <body> <p>' . $input->post->message . '</p> </body> </html>'; wireMail($contactFormRecipient, $input->post->email, "Contact Form | " . $input->post->name, $message); $session->flashMessage = 'Thank you for your message! We will get in touch with you shortly.'; $session->sent = true; $session->redirect($pages->get($contactPageID)->url); } else { $session->flashMessage = 'Error while validating you are not a robot!'; } } } ?> <div class="container"> <div class="row"> <div class=" col-md-4"> <h2>Contact Form</h2> <?php if($session->flashMessage):?> <div class="alert <?=!$session->sent && (!$v->validate() || !$resp->isSuccess()) ? 'alert-danger' : 'alert-success'?>" role="alert"> <?php echo $session->flashMessage;?> </div> <?php endif;?> <form id="contact-form" method="post"> <div class="form-group <?=$v->errors('name') ? 'has-error' : ''?>"> <label for="name">Name</label> <input class="form-control" name="name" id="name" type="text" value="<?=$sanitizer->text($input->post->name)?>"> </div> <div class="form-group <?=$v->errors('email') ? 'has-error' : ''?>"> <label for="email">Email</label> <input class="form-control" name="email" id="email" type="text" value="<?=$sanitizer->text($input->post->email)?>"> </div> <div class="form-group <?=$v->errors('message') ? 'has-error' : ''?>"> <label for="message">Message</label> <textarea class="form-control" name="message" id="message"><?=$sanitizer->text($input->post->message)?></textarea> </div> <div class="form-group"> <!-- Google Recaptcha code START --> <div class="g-recaptcha" data-sitekey="<?=$googleSiteKey?>"></div> <script type="text/javascript" src="https://www.google.com/recaptcha/api.js"> </script> <!-- Google Recaptcha code END --> </div> <button type="submit" class="btn btn-primary">SEND</button> </form> </div> </div> </div> <?php $session->remove('flashMessage'); $session->sent = false; ?>
  10. i will try to find some time to optimize and enrich the code a bit...
  11. Maybe not ... feel free to improve the code....
  12. @Macrura can i move it somehow or do i need help from a moderator?
  13. Glad you like it @szabesz !!
  14. Simple Contact Form Using Google Recaptcha & Valitron validation library I just finished creating a simple Contact-Form for a client's website so i thought it would be really helpfull to share it with the community. The contact form uses: Google recaptcha for validating the user is not a robot (https://github.com/google/recaptcha) Valitron validation library for validating the form fields (https://github.com/vlucas/valitron) Twitter Bootstrap 3.0.0 for form HTML The contact-form is located inside a contact-page, so the bare minimum you need in order to setup your own is: contact.php (template file used by your contact-page) _contact-controller.php (file used as a controller for your contact-form functionality like send email, validate fields etc) 2 extra lines inside your composer.json file So, let's start: First you need to update your composer.json file adding 2 lines inside the require object: "vlucas/valitron": "^1.2", "google/recaptcha": "~1.1" Here is a sample composer.json file: { "name": "processwire/processwire", "type": "library", "description": "ProcessWire CMS/CMF", "keywords": [ "cms","cmf", "content management system" ], "homepage": "https://processwire.com", "authors": [ { "name": "Ryan Cramer", "email": "ryan@processwire.com", "homepage": "https://processwire.com", "role": "Developer" } ], "require": { "php": ">=5.3.8", "ext-gd": "*", "vlucas/valitron": "^1.2", "google/recaptcha": "~1.1" }, "autoload": { "files": [ "wire/core/ProcessWire.php" ] }, "minimum-stability": "dev" } open console and navigate to processwire root folder (where composer.json file is) on this step i assume you have already setup composer for your project, otherwise google it run the following command: composer update this will create a vendor folder (if it does not already exist) and download valitron and google recaptcha libraries. Then open your contact-page template file(usually named contact.php inside your templates directory) and add the following: * Note: The form below uses bootstrap 3.0.0 css, so if you are using something else you need to make the appropriate changes. <?php namespace ProcessWire; include('_contact-controller.php') ?> <div class="container"> <div class="row"> <div class=" col-md-4"> <h2>Contact Form</h2> <?php if($session->flashMessage):?> <div class="alert <?=!$session->sent && (!$v->validate() || !$resp->isSuccess()) ? 'alert-danger' : 'alert-success'?>" role="alert"> <?php echo $session->flashMessage;?> </div> <?php endif;?> <form id="contact-form" method="post"> <div class="form-group <?=$v->errors('name') ? 'has-error' : ''?>"> <label for="name">Name</label> <input class="form-control" name="name" id="name" type="text" value="<?=$sanitizer->text($input->post->name)?>"> </div> <div class="form-group <?=$v->errors('email') ? 'has-error' : ''?>"> <label for="email">Email</label> <input class="form-control" name="email" id="email" type="text" value="<?=$sanitizer->text($input->post->email)?>"> </div> <div class="form-group <?=$v->errors('message') ? 'has-error' : ''?>"> <label for="message">Message</label> <textarea class="form-control" name="message" id="message"><?=$sanitizer->text($input->post->message)?></textarea> </div> <div class="form-group"> <!-- Google Recaptcha code START --> <div class="g-recaptcha" data-sitekey="<?=$googleSiteKey?>"></div> <script type="text/javascript" src="https://www.google.com/recaptcha/api.js"> </script> <!-- Google Recaptcha code END --> </div> <button type="submit" class="btn btn-primary">SEND</button> </form> </div> </div> </div> <?php //here we remove the flash-message because it is already shown above the form. $session->remove('flashMessage'); //reset 'sent' variable for future submit $session->sent = false; ?> Next create a file inside you templates directory with name: _contact-controller.php: and set the required variables($googleSiteKey, $contactFormRecipient, $contactPageID) <?php namespace ProcessWire; /** * here we include Valitron & Google recaptcha libraries * make sure the path is correct in your template */ include(dirname(__FILE__) . "/../../vendor/vlucas/valitron/src/Valitron/Validator.php"); include(dirname(__FILE__) . '/../../vendor/google/recaptcha/src/ReCaptcha/ReCaptcha.php'); /** * here we add the form field values to Valitron */ $v = new \Valitron\Validator(array( 'name' => $sanitizer->text($input->post->name), 'email' => $sanitizer->email($input->post->email), 'message' => $sanitizer->text($input->post->message), ) ); /** * validation rules set for each form field * For more details on Valitron/Validator usage visit: * https://github.com/vlucas/valitron */ $v->rule('required', ['name', 'email', 'message']); $v->rule('lengthMin', 'name', 5); $v->rule('email', 'email'); /** * set Google recaptcha site-key & secret-key * create a new key from: https://www.google.com/recaptcha/admin */ $googleSiteKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $googleSecretKey = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'; /** * set the email of the contact form recipient(usually the website owner) */ $contactFormRecipient = 'your@company.com'; /** * set the id of contact-page in order to redirect there when the form is sent */ $contactPageID = '1045'; //here we check whether the 'name' field exists inside post variables (which means the form is posted) if ($input->post->name) { //if fields validation passes if ($v->validate()) { $reCaptcha = new \ReCaptcha\ReCaptcha($googleSecretKey); $resp = $reCaptcha->verify($input->post->{'g-recaptcha-response'}, $_SERVER["REMOTE_ADDR"]); //if google-recaptcha validation passes if ($resp->isSuccess()) { //This is the HTML message $message = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Contact Form | ' . $input->post->name . '</title> </head> <body> <p>' . $input->post->message . '</p> </body> </html>'; //here we send the form to $contactFormRecipient wireMail($contactFormRecipient, $input->post->email, "Contact Form | " . $input->post->name, $message); //here we set a flash-message to notify the user that the form was successfully sent $session->flashMessage = 'Thank you for your message! We will get in touch with you shortly.'; //save in session that the form is sent $session->sent = true; //finally redirect user to contact-page $session->redirect($pages->get($contactPageID)->url); } else { //self explain $session->flashMessage = 'Error while validating you are not a robot!'; } } } ?> Thats all! You now have a simple contact-form working with captcha and field validation! I would be more than happy to help anyone having problems on the setup.
×
×
  • Create New...