skip to Main Content

I’ve been having an issue getting php to show a message when a form is submitted via email. I’ve googled it most of the day and tried a dozen or more fixes, but I cannot get it to show the message, it just jumps back to the previous page after execution, as per the header instructions in the php code. Any ideas (not ajax please).

Here’s the html that calls the php.

        <h2 class="u-text u-text-1">Questions or Comments?</h2>
        <div class="u-clearfix u-custom-html u-expanded-width u-custom-html-1">
          <form id="comment_form" action="form.php" method="post" style="none;width: 356px;padding: 10px;margin-top: 10px;float:top;margin-left: 40px;float: left;">
            <input type="email" placeholder="Enter your email" id="email" name="email" size="40">
            <br>
            <br><!--       <input type="email" placeholder="Type your email" size="40"><br><br>  -->
            <textarea name="Name" placeholder="Name" rows="1" cols="40"></textarea>
            <textarea name="comment" placeholder="Comments" rows="8" cols="80"></textarea>
            <br>
            <br>
            <input type="submit" name="submit" value="Submit">
            <br>
            <br>

I know this has been asked millions of times, but nothing I’ve googled will work.

Here’s the php.

<?php
        $email;$Name;$comment;$captcha;
        if(isset($_POST['email'])){
          $email=$_POST['email'];
        }if(isset($_POST['Name'])){
          $name=$_POST['Name'];          
        }if(isset($_POST['comment'])){
          $comment=$_POST['comment'];
        }if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
      }
        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
    $secretKey = "--nope-not showing it--";
    $ip = $_SERVER['REMOTE_ADDR'];

    // post request to server
    $url =  'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
    $response = file_get_contents($url);
    $responseKeys = json_decode($response,true);
    if($responseKeys["success"]) {
        $status = "Submission Successful";
//      echo '<h2>Thanks for posting comment</h2>';
        $subject = "New Form Submission";
        $to = "[email protected]";
        $comments = "name: $name rn email: $email rn $comment";
        $headers = "From: $email rn";
        mail($to,$subject,$comments,$headers);
    } else {
        $status = "Submission Error";
//      echo '<h2>You are a spammer ! Get the @$%K out</h2>';
        }
       
  if(isset($_REQUEST["destination"])){
      header("Location: {$_REQUEST["destination"]}");
  }else if(isset($_SERVER["HTTP_REFERER"])){
      header("Location: {$_SERVER["HTTP_REFERER"]}");
  }else{
       /* some fallback, maybe redirect to index.php */
  }

?>

2

Answers


  1. Chosen as BEST ANSWER

    Finally figured it out, thanks Justinas. I made a change in your header suggested statement to this and it works fine.

    header("refresh:5; url={$_SERVER['HTTP_REFERER']}");


  2. I will just assume, that problem is not because all echo is commented out and all conditions are working as expected.


    Since you can not use header() when there is output, you need to first set headers, and only then show text:

    header("refresh:5; url={$_REQUEST["destination"]}");
    
    /* ... */
    
    echo '<h2>Thanks for posting comment</h2>';
    
    

    This will start redirection after 5 seconds and only then show message (first process information and set message & url to use, then at the end set header and output message)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search