Alyona Yavorska
Alyona Yavorska

Reputation: 579

How to add to contact form "Your message has been received"

With purpose to save your time I have two screenshots of my contact form. enter image description hereenter image description here

Also instead of expected result the message "Thank you. Your message has been received" I got in new window. How can I place the message in that input element? Where am I wrong? You can check my contact form by this link just send any message. Thank you so much for your time and answers.

<form action="contact/form-handler.php" method="post">
            <fieldset>
              <div class="form-row">
              <div class="form-group col-md-6">
                    <input type="text" class="form-control" name="name" id="name" placeholder="Your Name"/>
                  </div>
                 <div class="form-group col-md-6">
                    <input type="text" class="form-control" name="email" id="email" placeholder="Your Email"/>
                  </div>
                  </div>         
               <div class="form-group">
                    <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject"/>
                  </div>
                 <div class="form-group">
                    <textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
                  </div>
                  <div>
                    <input type="hidden" name="hidden" value="" />

                       <div class="nocomment">
                    <label for="nocomment"></label>
                    <input id="nocomment" class="nocomment" value="" name="nocomment" />
                  </div>
                 <div class="text-center">
                    <input type="submit" value="Send Message" name="submit"/>
                       <input type="reset" value="Clear Message" name="reset"/>
                  </div>
                </div>

                <input type="hidden" name="v_error" id="v-error" value="Required" />
                <input type="hidden" name="v_email" id="v-email" value="Enter a valid email" />
            </fieldset>
          </form>

And my contact/form-handler.php

<?php
include('SMTPClass.php');
$emailto = 'myemail.com';

    // retrieve from parameters
    $emailfrom = isset($_POST["email"]) ? $_POST["email"] : "";
    $nocomment = isset($_POST["nocomment"]) ? $_POST["nocomment"] : "";
    $subject = 'Email from InWebWorld';
    $message = '';
    $response = '';
    $response_fail = 'There was an error verifying your details.';

        // Honeypot captcha
        if($nocomment == '') {

            $params = $_POST;
            foreach ( $params as $key=>$value ){

                if(!($key == 'ip' || $key == 'emailsubject' || $key == 'url' || $key == 'emailto' || $key == 'nocomment' || $key == 'v_error' || $key == 'v_email')){

                    $key = ucwords(str_replace("-", " ", $key));

                    if ( gettype( $value ) == "array" ){
                        $message .= "$key: \n";
                        foreach ( $value as $two_dim_value )
                        $message .= "...$two_dim_value<br>";
                    }else {
                        $message .= $value != '' ? "$key: $value\n" : '';
                    }
                }
            }

        $response = sendEmail($subject, $message, $emailto, $emailfrom);

        } else {

            $response = $response_fail;
        }

    echo $response;

// Run server-side validation
function sendEmail($subject, $content, $emailto, $emailfrom) {

    $from = $emailfrom;
    $response_sent = 'Thank you. Your messsage has been received.';
    $response_error = 'Error. Please try again.';
    $subject =  filter($subject);
    $url = "Origin Page: ".$_SERVER['HTTP_REFERER'];
    $ip = "IP Address: ".$_SERVER["REMOTE_ADDR"];
    $message = $content."\n$ip\r\n$url";

    // Validate return email & inform admin
    $emailto = filter($emailto);

    // Setup final message
    $body = wordwrap($message);

    // Create header
    $headers = "From: $from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=utf-8\r\n";
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";

    // Send email
    $mail_sent = @mail($emailto, $subject, $body, $headers);
    $response = $mail_sent ? $response_sent : $response_error;

    return $response;
}

// Remove any un-safe values to prevent email injection
function filter($value) {
    $pattern = array("/\n/", "/\r/", "/content-type:/i", "/to:/i", "/from:/i", "/cc:/i");
    $value = preg_replace($pattern, "", $value);
    return $value;
}

exit;

?>

Upvotes: 0

Views: 269

Answers (1)

hwnd
hwnd

Reputation: 137

If this php code is in contact/form-handler.php than instead of echo $response; you should redirect back to your contact page with the message, like
header('Location: contact_page_url?message='.urlencode($response));

And in your contact page put below your form:

<script>
    function findGetParameter(parameterName) {
        var result = null, tmp = []; 
        location.search.substr(1).split("&").forEach(function (item){ 
            tmp = item.split("="); 
            if (tmp[0] === parameterName) 
                result = decodeURIComponent(tmp[1]); 
        });
        return result;
    }
    document.querySelector('#nocomment').value = findGetParameter('message') || '';            
</script>

Upvotes: 2

Related Questions