webhoes Posted November 10, 2017 Share Posted November 10, 2017 Hello, I noticed an issue with my mail form. When I am logged in it works. When I am not logged in it doesn't work. The templates are chached so I presume it must be that. Below is my code. Could it be cache or something else. And more important, how to should I fix this. <?php if (isset($_POST["submit"])) { $result = '<div class="alert alert-success">Form submitted</div>'; if (!$_POST['name']) { $error = "<br />Vul je naam in"; } if (!$_POST['email']) { $error .= "<br />Vul je emailaddress in"; } if (!$_POST['comment']) { $error .= "<br />Vul een opmerking in"; } if ($_POST ['email'] != "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $error .= "<br />Gebruik een geldig emailaddress"; } if (isset($error)) { $result = '<div class="alert alert-danger"><strong>Da hej mangs, er is iets niet goed ingevuld:</strong> ' . $error . '</div>'; } else { $message = $mail->new(); $message->subject('mail via webhoes.nl') ->to('sanne@webhoes.nl') ->from(strip_tags($_POST['email'])) ->fromName(strip_tags($_POST['name'])) ->body('Name: ' . strip_tags($_POST['name']) . "\r\n" . 'Email: ' . strip_tags($_POST['email']) . "\r\n" . 'Comment: ' . strip_tags($_POST['comment'])); $numSent = $message->send(); $result = '<div class="alert alert-success"><strong>Bedankt voor je email. Wij nemen zo spoedig mogelijk contact op.</strong></div>'; } echo $result; } ?> <!-- Button trigger modal --> <!-- Modal --> <div class="modal fade slide left" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> </button> <p class="modal-title" id="myModalLabel">Contact met Webhoes!</p> </div> <div class="modal-body"> <p class="lead">Laat wat van je horen!</p> <form method="post" id="myForm"> <div class="form-group"> <label for="name">Naam:</label> <input type="text" name="name" id="name" class="form-control" placeholder="Name" value="" required/> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" name="email" id="email" class="form-control" placeholder="Email" value="" required/> </div> <div class="form-group"> <label for="comment">Opmerking:</label> <textarea class="form-control" id="comment" name="comment" required></textarea> </div> <input type="submit" name="submit" class="btn btn-success btn-lg" value="Verstuur"> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-error" data-dismiss="modal">Annuleren</button> </div> </div> </div> </div> Link to comment Share on other sites More sharing options...
Zeka Posted November 10, 2017 Share Posted November 10, 2017 Hi @webhoes You can set "Cache disabling POST variables" in template setting ( Cache tab ). Or you can create a separate template -> page without cache enabled and post your form via ajax to that URL. Link to comment Share on other sites More sharing options...
rafaoski Posted November 10, 2017 Share Posted November 10, 2017 Hi @webhoes ... I changed the code using sanitizer and downloaded $ _POST using input->post ... I added also at the top of the namespace Processwire and <form action ='./' ... Code below for me to work: <?php namespace Processwire; ?> <div id='content-body' class="page-b" pw-append> <?php if (isset($_POST["submit"])) { $result = '<div class="alert alert-success">Form submitted</div>'; if (!$_POST['name']) { $error = "<br />Vul je naam in"; } if (!$_POST['email']) { $error .= "<br />Vul je emailaddress in"; } if (!$_POST['comment']) { $error .= "<br />Vul een opmerking in"; } if ($_POST ['email'] != "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $error .= "<br />Gebruik een geldig emailaddress"; } if (isset($error)) { $result = '<div class="alert alert-danger"><strong>Da hej mangs, er is iets niet goed ingevuld:</strong> ' . $error . '</div>'; } else { // MAIL NEW https://processwire.com/api/ref/mail/new/ // SANITIZER https://processwire.com/api/variables/sanitizer/ // INPUT https://processwire.com/api/variables/input/ // YOUR MAIL $your_email = 'yormail@gmail.com'; // OR GET FROM CONTACT PAGE // $your_email = $sanitizer->email($page->email); // LIKE strip_tags($_POST['email']) // SANITIZE MESSAGE $email = $sanitizer->email($input->post->email); // LIKE strip_tags($_POST['email']) $name = $sanitizer->text($input->post->name); // LIKE trip_tags($_POST['name']) $comment = $sanitizer->text($input->post->comment); // LIKE trip_tags($_POST['comment']) $message = $mail->new(); $message->to($your_email)->from($email); $message->subject('Mail Subject') ->body('Name: ' . $name . "\r\n" . 'Email: ' . $email . "\r\n" . 'Comment: ' . $comment) ->bodyHTML('Name: ' . $name . "\r\n" . 'Email: ' . $email . "\r\n" . 'Comment: ' . $comment); $numSent = $message->send(); $result = '<div class="alert alert-success"><strong>Wysłałeś wiadomość.</strong></div>'; } echo $result; } ?> <!-- Button trigger modal --> <!-- Modal --> <div class="modal fade slide left" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> </button> <p class="modal-title" id="myModalLabel">Contact met Webhoes!</p> </div> <div class="modal-body"> <p class="lead">Laat wat van je horen!</p> <form action='./' method="post" id="myForm"> <div class="form-group"> <label for="name">Naam:</label> <input type="text" name="name" id="name" class="form-control" placeholder="Name" value="" required/> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" name="email" id="email" class="form-control" placeholder="Email" value="" required/> </div> <div class="form-group"> <label for="comment">Opmerking:</label> <textarea class="form-control" id="comment" name="comment" required></textarea> </div> <input type="submit" name="submit" class="btn btn-success btn-lg" value="Verstuur"> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-error" data-dismiss="modal">Annuleren</button> </div> </div> </div> </div> </div> 1 Link to comment Share on other sites More sharing options...
webhoes Posted November 11, 2017 Author Share Posted November 11, 2017 Thanks @Zeka I disabled caching post variables. It seems to work nog. @rafaoski your are right to use sanitizer. I will change that too. There is already a namespace on top of _main.php. My code is also in _main.php. Did you put the code in another file (template)? Link to comment Share on other sites More sharing options...
szabesz Posted November 11, 2017 Share Posted November 11, 2017 6 minutes ago, webhoes said: your are right to use sanitizer. I will change that too. While you are at it, I also recommend https://processwire.com/api/ref/input/ over isset($_POST["submit"] etc... After all, we have an API at our disposal 1 Link to comment Share on other sites More sharing options...
rafaoski Posted November 11, 2017 Share Posted November 11, 2017 @webhoes I am using a new template strategy from this article: https://processwire.com/blog/posts/processwire-3.0.49-introduces-a-new-template-file-strategy/ I put php code in both home.php, contact.php and _main.php.I tested using Processwire 3.0.83 DEV on Hosting ...You can even use this profile on which I tested or worked:https://github.com/rafaoski/site-twilight 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