0

When I open the email the message is always empty, I get the name and email but message is empty, I recently added google recaptcha v3 which was giant pain , but It is now working. anyway I tried to debug this, and It show no errors, the message textarea send to email but I receive it empty...

this is the form

<?php

if(isset($_POST['submit'])){
  $captcha=$_POST['token'];
  $secret   = 'censord';
  $response = file_get_contents(
      "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']
  );
  // use json_decode to extract json response
  $response = json_decode($response);

if($response->success == true) {
    //save data to database
    echo '<div class="alert alert-success">
    <strong>Success!</strong> Data is saved.
    </div>';
} else {
    echo '<div class="alert alert-warning">
    <strong>warning!</strong> Failed to save data.
    </div>';
}

}
?>
    <style>
    </style>
  </head>

  <body>

<?php
  if(isset($_POST['submit'])){

    $name = htmlspecialchars(stripslashes(trim($_POST['username'])));
    $email = htmlspecialchars(stripslashes(trim($_POST['email'])));

    $text = htmlspecialchars(stripslashes(trim($_POST['textt'])));
    if(!preg_match("/^[A-Za-z .'-]+$/", $name)){
      $name_error = 'Invalid name';
    }

    if(!preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/", $email)){
      $email_error = 'Invalid email';
    }
    if(strlen($text) === 0){
      $message_error = 'Your message should not be empty';
    }
    if(preg_match('/http|www/i',$comments)) {
    $error_message .= "We do not allow a url in the comment.<br />";
  }
  }
?>

  <div id="container">
    <div class="form-wrap">
         <h1>Report bug</h1>
        <p>Report us if you find any bug or broken link in our free online programming classes.</p>

        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

            <div class="form-group">

                <label for="username">Name:</label>
                <input type="text" name="username">
   <p><?php if(isset($name_error)) echo $name_error; ?></p>

            </div>

            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" name="email">
                  <p><?php if(isset($email_error)) echo $email_error; ?></p>

            </div>

            <div class="form-group">
                <label for="textt">Message:</label>
                <textarea type="text" name="textt" rows="22" cols="42"></textarea>
   <p><?php if(isset($message_error)) echo $message_error; ?></p>

</div>

                <button type="submit" name="submit" value="Submit" class="btn">Report us</button>
    <input type="hidden" name="action" value="validate_captcha">

 <?php 
    if(isset($_POST['submit']) && !isset($name_error) && !isset($subject_error) && !isset($email_error) && !isset($message_error)){
      $to = 'censord'; // edit here
      $body = " Name: $name\n E-mail: $email\n Message:\n $message";
      if(mail($to, $subject, $body)){
        echo '<p style="color: green">Message sent</p>';
      }else{
        echo '<p>Error occurred, please try again later</p>';
      }
    }
  ?>

        </form>
      </div>

    </div>

  </body>
  <script>
grecaptcha.ready(function() {
    grecaptcha.execute('censord', {action: 'validate_captcha'}).then(function(token) {
       console.log(token);
       document.getElementById("token").value = token;
    });
});
</script>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
  • 2
    This code is not the most legible - but it looks like you've assigned the message to a variable called `$text` **not** `$message` – CD001 Jan 03 '20 at 16:46
  • The code is badly arranged - why are there two separate PHP blocks separated by some HTML, even though they are part of the same process flow? And you don't need to check _twice_ whether the form is submitted or not! It's messy and hard to follow. But CD001 is right - you capture the input of the textarea into a PHP variable called `$text`...but then you never use it for anything. The variable you put into the email is called `$message` instead, and as far as I can see you never assign a value to that. `$body = " Name: $name\n E-mail: $email\n Message:\n $text";` will probably fix the problem. – ADyson Jan 03 '20 at 16:48
  • 1
    Another FYI: I would look at your apache (server) logs for any 404 errors. An anatics package can assist you here. You might also think of a custom 404 page as well, which might do some logging via a database which you can then look into. Also a look into your php errors log would also assist you in finding any errors in your site. In my experience it's rare for people to come into an error and then go over to the contact page to let the site know. – Richard Housham Jan 03 '20 at 16:53
  • what? my form is working good, the anti spam captcha working, I just want to fix the message, I don't care if the php work flow is perfect – SarahMilner Jan 03 '20 at 17:03
  • anyways Richard housham thanks for fixing it – SarahMilner Jan 03 '20 at 17:10
  • @SarahMilner huh? Richard Housham's comment was suggesting a better way to find bugs than asking for user feedback. It doesn't fix the problem in your question. The comments from me and CD001 are the ones which would actually resolve the error with your form. (You might not care right now if the code is badly structured, but it'll make it easier to read and maintain in future if you improve it. And if you write more complex pieces of code, then you'll _need_ a good structure otherwise it becomes unmanageable very quickly.) – ADyson Jan 03 '20 at 17:34
  • sorry, thank you adyson. – SarahMilner Jan 03 '20 at 19:19

0 Answers0