php - Unable to replace form with message on submission -


i've created html5 form, incorporates recaptcha, , i've written php script sends email when form submitted. @ moment, script redirects user error or thankyou page, i'm trying adjust dynamically replace form within message within same page.

i've tried following script, displays message page loads, before user interaction.

php/html:

<?php if ($_post) {  // load recaptcha library include_once ("autoload.php");  $name = trim(stripslashes($_post['name'])); $email = trim(stripslashes($_post['email'])); $message = trim(stripslashes($_post['message'])); $emailfrom = $email; $emailto = "my@email.com"; $subject = "contact request";  // prepare email body text $body = "<strong>name:</strong> $name <br /> <strong>email:</strong> $email <br /> <strong>message:</strong> $message";  $headers .= 'mime-version: 1.0' . "\r\n"; $headers .= 'content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "from: $name <$emailfrom>" . "\r\n";  $secret = 'xxx'; $recaptcha = new \recaptcha\recaptcha($secret); $resp = $recaptcha->verify($_post['g-recaptcha-response'],$_server['remote_addr']);  echo 'your message submitted!';  } else { ?> <div class="contact-form">     <form role="form" method="post" action="index.php">         <label for="name"><span>name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="please enter name." /></label>         <label for="email"><span>email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="please enter email address." /></label>         <label for="message"><span>message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="please enter message."></textarea></label>         <label><span>&nbsp;</span><div id="recaptcha"><div class="g-recaptcha" data-sitekey="6lcbawstaaaaakbpfgs1japxnrlvr2mipng0fxol"></div></div></label>         <label><span>&nbsp;</span><input type="submit" value="" class="submit-button" /></label>                       </form>              </div> <?php  } ?> 

i'm new php, i'm not sure if it's syntax or semantics issue. appreciated!

here's 1 way of doing it.

check see if form has been submitted if(isset($_post['submit'])). can use if($_server['request_method'] == 'post') see if form has been submitted.

then check if email has been sent, , if has set $success_message variable.

we check see if $success_message variable set, , if isn't, show form.

also, note added name="submit" submit button element. how we're checking see if form has been submitted.

i changed stripslashes() strip_tags() prevent malicious code getting through.

<?php  // load recaptcha library include_once ("autoload.php");  if(isset($_post['submit'])) {      $name = trim(strip_tags($_post['name']));     $email = trim(strip_tags($_post['email']));     $message = trim(strip_tags($_post['message']));     $emailfrom = $email;     $emailto = "my@email.com";     $subject = "contact request";      // prepare email body text     $body = "<strong>name:</strong> $name <br /> <strong>email:</strong> $email <br /> <strong>message:</strong> $message";      $headers  = 'mime-version: 1.0' . "\r\n";     $headers .= 'content-type: text/html; charset=iso-8859-1' . "\r\n";     $headers .= "from: $name <$emailfrom>" . "\r\n";      $secret = 'xxx';     $lang = 'en';     $recaptcha = new \recaptcha\recaptcha($secret);     $resp = $recaptcha->verify($_post['g-recaptcha-response'],$_server['remote_addr']);       // edit: repositioned recaptcha op's pastebin script, requested , adjusted messaging     // changed $success var $message , added error message      // original if statement, redirected user     if($resp->issuccess()){          // send email         if(mail($emailfrom, $subject, $body, $headers)) {              // set success message             $success_message = 'the form sent! yay!';          } else {              // error message             $error_message = 'could not send email';         }     } else {         $error_message = 'prove human!';     } } ?> <div>     <!-- quick , dirty way print messages -->     <?php if(isset($success_message)) { echo $success_message; } ?>     <?php if(isset($error_message)) { echo $error_message; } ?> </div>  <?php if(!isset($success_message)): ?> <div class="contact-form">     <form role="form" method="post" action="index.php">         <label for="name"><span>name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="please enter name." /></label>         <label for="email"><span>email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="please enter email address." /></label>         <label for="message"><span>message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="please enter message."></textarea></label>          <div class="g-recaptcha" data-sitekey="6lcbawstaaaaakbpfgs1japxnrlvr2mipng0fxol"></div>         <script type="text/javascript"                 src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>">         </script>          <label><span>&nbsp;</span><input type="submit" name="submit" value="" class="submit-button" /></label>                       </form>              </div> <?php endif; ?> 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -