Christophe Posted February 4, 2015 Share Posted February 4, 2015 Hello, I'm not sure it's the right place to post this topic. I've just converted a Joomla! 1.5 website to Processwire 2.5.3. I didn't want to do some training for Joomla! for someone for whom I had made it years ago and who hasn't really edited/managed it since then. As I'm going to start some training tomorrow, I wanted to use Processwire. I have used a basic form based on some code found in an old topic of this forum on 1-2 websites already. They use the "Direct Output with Includes" approach. But for this one that I've put online today, I'm using the default intermediate profile as a base. (For another website in development, the multilingual profile is used.) So I've tried to insert the form in several ways in a Contact page, but due to the "Delayed output" approach it's more difficult than with the other approach. So I've finally installed the Hanna Code module that I'm using for the first time. I had to uninstall it once as I had a "Hello World" text appearing on the bottom of each page. I wasn't sure why, but apparently installing the Hanna Code module also installed the Hello World module and made this text appear. So, to come back to the reason I'm posting this, I have a hanna code named "contact_form" (that is inserted in the sidebar textarea editor of a Contact page - not sure for the moment if I'm going to use the basic-page template or the contact (contact.php) one): <?php /** * Example of a simple contact form in ProcessWire * */ // set this to the email address you want to send to (or pull from a PW field) $emailTo = 'whatever@gmail.com'; // or if not set, we'll just email the default superuser if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; // set and sanitize our form field values $form = array( 'Nom complet ' => $sanitizer->text($input->post->fullname), 'E-mail ' => $sanitizer->email($input->post->email), 'Message ' => $sanitizer->textarea($input->post->comments), ); // initialize runtime vars $sent = false; $error = ''; // check if the form was submitted if($input->post->submit) { // determine if any fields were ommitted or didn't validate foreach($form as $key => $value) { if(empty($value)) $error = "<h3 class='error'>Veuillez vérifier que vous avez complété tous les champs.</h3>"; } // if no errors, email the form results if(!$error) { $subject = "Formulaire de contact"; $message = ''; foreach($form as $key => $value) $message .= "$key: $value\n"; mail($emailTo, $subject, $message, "From: $form[email]"); $sent = true; } } if($sent) { echo "<h3>Merci, votre message a été envoyé.</h3>"; // or pull from a PW field } else { // encode values for placement in markup foreach($form as $key => $value) { $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); } // output the form echo <<< _OUT $error <form action="./" method="post"> <p> <label for="fullname">Vos Prénom et Nom</label><br /> <input type="text" id="fullname" name="fullname" size="46" value="$form[fullname]" /> </p> <p> <label for="email">Votre e-mail</label><br /> <input type="email" name="email" id="email" size="46" value="$form[email]" /> </p> <p> <label for="comments">Message</label><br /> <textarea id="comments" name="comments" rows="5" cols="60">$form[comments]</textarea> </p> <p><input type="submit" name="submit" value="Envoyer" /></p> </form> _OUT; } (I also tried with a small php code with "include" in it and the previous code in a php file. I don't remember if it worked at least the same. Perhaps it was better for security reasons (?).) So, I have several questions/issues . # Is this form still "secure" with Processwire 2.5.3? And enough "secure" inserted from a Hanna code? # In the "Code" tab of the hanna code, I'm having an error message when hovering the cross next to line 38 : mail($emailTo, $subject, $message, "From: $form[email]"); The message is: "syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING". I've tried some things but it hasn't made the cross disappear. # Also, in the "Test Results" tab I have this: Notice: Undefined index: fullname in /home/cyberbodw/www/site/assets/cache/HannaCode/contact_form.php on line 62 Notice: Undefined index: email in /home/cyberbodw/www/site/assets/cache/HannaCode/contact_form.php on line 67 Notice: Undefined index: comments in /home/cyberbodw/www/site/assets/cache/HannaCode/contact_form.php on line 72 # The email message is well received but seems to be received 2 times in an interval of a few seconds. # The sender's email address doesn't appear in the "From" field (but it does appear in the message itself). Something like "(Unknown sender)" is written instead. (Perhaps related to the line of code next to the cross.) If it's not possible to have the sender's email address, is it possible to have something like "Contact form" in the "From" field, and how? (It's what appears in the "Subject" field currently.) # There is this: // set this to the email address you want to send to (or pull from a PW field) $emailTo = 'whatever@gmail.com'; // or if not set, we'll just email the default superuser if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; How can I pull it from a second user (only view and edit rights for the moment) that is not the superuser? From a PW Field, and how? Is there a "superUserPageID" "equivalent" for the second user? Is "superUserPageID" used directly (it seems) or does it have to be replaced by the real ID? Can we find the second user('s) page ID, and, if yes, how? # Perhaps I'm forgetting one... NB: I know why I don't write often , it takes me too much time. I wonder how much time it takes (in general of course and more or less) for other people for average to large posts. Thank you in advance 2 Link to comment Share on other sites More sharing options...
Christophe Posted February 12, 2015 Author Share Posted February 12, 2015 Someone ? Link to comment Share on other sites More sharing options...
Jan Romero Posted February 12, 2015 Share Posted February 12, 2015 Hey man. That’s a huge post and I don’t have a lot of time, but I have a feeling correcting the array keys in $form will fix a bunch of it. For example: mail($emailTo, $subject, $message, "From: $form[email]"); The error message you get refers to the $form part. See what's wrong? email isn’t a thing. You have to give it a string as a key You have the same issue every time you refer to the $form array. Just put quotation marks around it and you’re almost there. The next thing is that the keys you try to access are not the ones you set here: $form = array( 'Nom complet ' => $sanitizer->text($input->post->fullname), 'E-mail ' => $sanitizer->email($input->post->email), 'Message ' => $sanitizer->textarea($input->post->comments), ); So you have to decide whether you want to use $form['Nom complet '] or $form['fullname'], $form['E-Mail '] or $form['email'], etc. Also, watch out for those spaces. They should work fine, if you want to go with those keys, but they’re pretty ugly. Disclaimer: I haven’t read your full post yet :< Edit: By the way, I'm not sure, but it might be that this will not parse correctly: "From: $form['email']" I suspect PHP might stop reading the variable after $form. For better control over complex variables inside literals, you can use curly braces like so: "From: {$form['email']}" Link to comment Share on other sites More sharing options...
Christophe Posted February 13, 2015 Author Share Posted February 13, 2015 Hi, And thank you for your help. I'll look at your message closely again. I'm just starting to use PHP a bit more with Processwire, and I just deduce/"hack"/tweak some things whenever I can. I didn't change the form a lot. I had tried 'email' but it hadn't changed anything, if I remember well. Perhaps I have to do it for every "input". I added the spaces because of the following ":". In French, a space is also needed before it. (Also, perhaps I should put a special character for the "é" of "Prénom".) On the other website, not using Hanna Code but a template, I hadn't noticed this problem. Also I don't remember receiving twice the message* at the same gmail account used for testing purposes. (For another case, perhaps I'll need to have a real "Subject", or "Formulaire de contact" : a real "Subject", instead of just "Formulaire de contact".) I'd like to have the real "From" and not "Unknown sender", if possible. In gmail, it's a bit like this*: Formulaire de contact via 81.mail-out.ovh.net Nom complet : First name Last nameE-mail : name@domain.tldMessage : The content of the message. via 81.mail-out.ovh.net ... Nom complet : First name Last nameE-mail : name@domain.tldMessage : The content of the message. Link to comment Share on other sites More sharing options...
Christophe Posted April 10, 2015 Author Share Posted April 10, 2015 Hi, I think I'll put back this code in a .php file. But the HannaCode module has enabled me to be aware of some errors. And apparently, I can't use a dash or a whitespace with $form. I've modified the code to be: <?php /** * Example of a simple contact form in ProcessWire * */ // set this to the email address you want to send to (or pull from a PW field) $emailTo = 'whatever@gmail.com'; // or if not set, we'll just email the default superuser if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; // set and sanitize our form field values $form = array( 'Nom' => $sanitizer->text($input->post->fullname), 'Courriel' => $sanitizer->email($input->post->email), 'Message' => $sanitizer->textarea($input->post->comments), ); // initialize runtime vars $sent = false; $error = ''; // check if the form was submitted if($input->post->submit) { // determine if any fields were ommitted or didn't validate foreach($form as $key => $value) { if(empty($value)) $error = "<h4 class='error'>Veuillez vérifier que vous avez complété tous les champs.</h4>"; } // if no errors, email the form results if(!$error) { $subject = "Formulaire de contact"; $message = ''; foreach($form as $key => $value) $message .= "$key : $value\n"; mail($emailTo, $subject, $message, "From: {$form[Courriel]}"); $sent = true; } } if($sent) { echo "<h3>Merci, votre message a été envoyé.</h3>"; // or pull from a PW field } else { // encode values for placement in markup foreach($form as $key => $value) { $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); } // output the form echo <<<_OUT $error <form action="./" method="post"> <p> <label for="fullname">Vos Prénom et Nom</label><br /> <input type="text" id="fullname" name="fullname" size="46" value="$form[Nom]" /> </p> <p> <label for="email">Votre e-mail</label><br /> <input type="email" name="email" id="email" size="46" value="$form[Courriel]" /> </p> <p> <label for="comments">Message</label><br /> <textarea id="comments" name="comments" rows="5" cols="60">$form[Message]</textarea> </p> <p><input type="submit" name="submit" value="Envoyer" /></p> </form> _OUT; } But I still receive the message twice when completing the form in Firefox, and Chrome. But only once from Opera (a recent version also). EDIT: Opera twice also... Does someone have an idea? I'm doing some research... Also, does this form seem secure? Thanks in advance NB: @Jan Romero -> Thank you for the curly braces... Link to comment Share on other sites More sharing options...
Christophe Posted April 15, 2015 Author Share Posted April 15, 2015 Anyone ? Link to comment Share on other sites More sharing options...
cstevensjr Posted April 15, 2015 Share Posted April 15, 2015 Also, does this form seem secure? I don't really know what your definition or concept of secure is, however I always ensure that any website that deals with important, privileged, proprietary or privacy information has some type of professional SSL Certificate attached to it. // or if not set, we'll just email the default superuser if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; I'm not a expert on code, however if you are receiving 2 messages, then this code (syntax) may not be working correctly or as you expected. Link to comment Share on other sites More sharing options...
Christophe Posted April 15, 2015 Author Share Posted April 15, 2015 Hello cstevensjr, Hope you're fine. In this case, enough secure not to risk compromising the website via it I suppose. I thought about this form again yesterday. I've just tried to comment out if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; but it doesn't change anything apparently. EDIT: online debuggers say I have a problem (starting) at the following line: 'Nom' => $sanitizer->text($input->post->fullname), Ex.: PHP Notice: Undefined variable: sanitizer... PHP Fatal error: Call to a member function text() on null in... Or Fatal error: Call to a member function text() on a non-object on line... I don't know if it's related in some way to the issue I have. 1 Link to comment Share on other sites More sharing options...
cstevensjr Posted April 15, 2015 Share Posted April 15, 2015 Hopefully someone with more coding skills will comment on your code. Thanks for clarifying on security. Link to comment Share on other sites More sharing options...
Macrura Posted April 15, 2015 Share Posted April 15, 2015 Hello cstevensjr, Hope you're fine. In this case, enough secure not to risk compromising the website via it I suppose. I thought about this form again yesterday. I've just tried to comment out if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; but it doesn't change anything apparently. EDIT: online debuggers say I have a problem (starting) at the following line: 'Nom' => $sanitizer->text($input->post->fullname), Ex.: PHP Notice: Undefined variable: sanitizer... PHP Fatal error: Call to a member function text() on null in... Or Fatal error: Call to a member function text() on a non-object on line... I don't know if it's related in some way to the issue I have. we would need to see the output, not only the php code - i don't think there is anything in your code that would cause the form to submit twice; it probably has to do with how you are using the hanna code; can you post the html output? this form posts to itself, so somewhere in there some variable is allowing it to process twice. this PHP file also needs to have access to the api since you are using api vars. http://cheatsheet.processwire.com/ so it can't be a file just sitting in the root, in case that's what you are doing - it needs to be a template file; you might want to test this not using a hanna code and see if that works; you could simply make a template for the contact page and then include that file there. 2 Link to comment Share on other sites More sharing options...
Christophe Posted April 17, 2015 Author Share Posted April 17, 2015 Hello Macrura, It's the only case where I'm using the hanna code. I'm going to use a .php template file like I wanted at first and like on other websites. On 2 other websites, that are at the same hosting company, one only receives it once when the other twice. I have to update the code everywhere and compare other things to test. On some other websites, I will try to use Simple Contact Form by justb3a, or the "system" that can be found in the Blue-VR ProcessWire Site Profile by Gayan Virajith (if someone can find a solution to the "issue" that I mentioned at https://processwire.com/talk/topic/925-german-de-de/?p=87848, and more than once at https://processwire.com/talk/topic/6647-blue-vr-processwire-site-profile/page-2#entry93155). NB: and now I've found again that I have saved an alternative simple form also taken from the forums... with similar and different parts... so it's a little confusing. EDIT: updated the form on 2 websites, it seems ok now. Will see how it is/will be when I go back from hanna code to a .php template file with the last one. Link to comment Share on other sites More sharing options...
Macrura Posted April 17, 2015 Share Posted April 17, 2015 what are your settings for the submit - does it stay on the same page, or do you redirect to another page? where does your form submit to (action) - self or some other page? main thing when using hanna code for a form is that if you are posting to the self page, the textformatter might somehow cause the form to submit 2x, upon submit Link to comment Share on other sites More sharing options...
Christophe Posted April 20, 2015 Author Share Posted April 20, 2015 Hello Macrura, It stays on the same page with a thank you message replacing the form. The form is submitted to a specified email address, and I commented out the following code as I don't need it: // or if not set, we'll just email the default superuser // if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; I will avoid using hanna code(s) for forms now. I'm currently building a small website based on the default profile's structure but with this: http://purecss.io/layouts/side-menu/ So, in _main.php, I've got: <div class="content"> <?php echo $content; ?> <?php if($page->template == 'contact') { include('./_form.php'); echo $map->render($page, 'map'); echo $page->child('name=informations, include=hidden')->body; } ?> </div> (The indentation was not kept as is by the forum text editor.) And, in contact.php, just this: <?php // contact.php (contact page) template file // Primary content is the page body copy $content = $page->body; $map = $modules->get('MarkupGoogleMap'); The form works well, but with this above the thank you message (but only in debug mode): "Notice: Use of undefined constant Courriel - assumed 'Courriel' in /home/website_name/www/site/templates/_form.php on line 38" So, I guess it's ok. Link to comment Share on other sites More sharing options...
giannisok Posted September 6, 2016 Share Posted September 6, 2016 Check my post here: Link to comment Share on other sites More sharing options...
Christophe Posted September 6, 2016 Author Share Posted September 6, 2016 Hello @giannisok, I now use FormBuilder, for simple or more complex forms. If I ever need to use something other than FormBuilder for a website one day, I'll surely take a close look at your solution. Thanks. Have a nice week! 1 Link to comment Share on other sites More sharing options...
giannisok Posted September 6, 2016 Share Posted September 6, 2016 Thanks @Christophe! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now