Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:08:45+00:00 2026-06-14T09:08:45+00:00

I have a php contact form which all works well with its validation etc,

  • 0

I have a php contact form which all works well with its validation etc, but I have one niggle with it. The original code which I have tweaked redirected the page to a new one with a thank you message on submission which I was unhappy with, so I’ve managed to get a thank you message to display on the original page, however the input form content still stays, i’d rather it didn’t. Even better I’d like to be able to hide the form completely and replace it with a thank you.

I ought to mention that when completed it will be placed on a page with other items, so it is just the form that I’m after hiding, or clearing.

This is the code in the Header

<?php 
$your_email ='email@example.com';

session_start();
$errors = '';
$name = '';
$company = '';
$visitor_email = '';
$phone = '';
$user_message = '';

if(isset($_POST['submit']))
{

$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$user_message = $_POST['message'];
///------------Do Validations-------------
if(empty($name)||empty($visitor_email))
{
    $errors .= "\n Name and Email are required fields. ";   
}
if(IsInjected($visitor_email))
{
    $errors .= "\n Bad email value!";
}
if(empty($_SESSION['6_letters_code'] ) ||
  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
    $errors .= "\n The captcha code does not match!";
}

if(empty($errors))
{
    //send the email
    $to = $your_email;
    $subject="New form submission";
    $from = $your_email;
    $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

    $body = "A user  $name submitted the contact form:\n".
    "Name: $name\n".
    "Company: $company\n".

    "Email: $visitor_email \n".
    "Phone: $phone\n".

    "Message: \n ".
    "$user_message\n".
    "IP: $ip\n";    

    $headers = "From: $from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";

    mail($to, $subject, $body,$headers);

    //header('Location: #thanks');
    $myForm =  '< style="visibility: hidden;">';
    $thankyou = file_get_contents("thank-you.html"); 
}
}

// Function to validate against any email injection attempts
function IsInjected($str)
{
 $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}


?>

And this is the form itself minus a chunk of javascript validation which I didn’t think was relevant to the question

<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id="footer">
    <div class="twelve-column-wrapper">
        <div class="six-column-wrapper">
            <div class="six-column">
                <h3>Why not get in touch</h3>
            </div>
            <div id='contact_form_errorloc' class='err'></div>
            <form method="POST" name="contact_form" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
                <div class="three-column">
                    <p>
                        <label for='name'>Name: </label>
                        <br>
                        <input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
                    </p>
                </div>
                <div class="three-column">
                    <p>
                        <label for='company'>Your Company: </label>
                        <input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
                    </p>
                </div>
                <div class="three-column">
                    <p>
                        <label for='email'>Email: </label>
                        <br>
                        <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
                    </p>
                </div>
                <div class="three-column">
                    <p>
                        <label for='phone'>Phone No. </label>
                        <input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
                    </p>
                </div>
                <div class="six-column">
                    <p>
                        <label for='message'>Message:</label>
                        <br>
                        <textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
                    </p>
                </div>
                <div class="three-column">
                    <p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
                        <label for='message'>Enter the code above here :</label>
                        <br>
                        <input id="6_letters_code" name="6_letters_code" type="text">
                        <br>
                        <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
                    <input id="submit"  type="submit" value="Submit" name='submit'>
                </div>
                <div class="six-column"> <?php echo $thankyou; ?> </div>
            </form>
        </div>
    </div>
</div>

I have tried a few methods I’ve found by searching but have fallen down mainly due to my basic knowledge of PHP.

Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:08:46+00:00Added an answer on June 14, 2026 at 9:08 am
    <?php
    if(!empty($errors)){
    echo "<p class='err'>".nl2br($errors)."</p>";
    }
    ?>
    <div id="footer">
        <div class="twelve-column-wrapper">
            <div class="six-column-wrapper">
                <div class="six-column">
                    <h3>Why not get in touch</h3>
                </div>
                <div id='contact_form_errorloc' class='err'></div>
    <?php
    if(!isset($_POST['submit'])):
    ?>
                <form method="POST" name="contact_form" 
    action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
                    <div class="three-column">
                        <p>
                            <label for='name'>Name: </label>
                            <br>
                            <input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
                        </p>
                    </div>
                    <div class="three-column">
                        <p>
                            <label for='company'>Your Company: </label>
                            <input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
                        </p>
                    </div>
                    <div class="three-column">
                        <p>
                            <label for='email'>Email: </label>
                            <br>
                            <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
                        </p>
                    </div>
                    <div class="three-column">
                        <p>
                            <label for='phone'>Phone No. </label>
                            <input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
                        </p>
                    </div>
                    <div class="six-column">
                        <p>
                            <label for='message'>Message:</label>
                            <br>
                            <textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
                        </p>
                    </div>
                    <div class="three-column">
                        <p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
                            <label for='message'>Enter the code above here :</label>
                            <br>
                            <input id="6_letters_code" name="6_letters_code" type="text">
                            <br>
                            <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
                        <input id="submit"  type="submit" value="Submit" name='submit'>
                    </div>
    
                </form>
    <?php
    endif;
    ?>
                <div class="six-column"> <?php echo $thankyou; ?> </div>
            </div>
        </div>
    </div>
    

    That will hide the form. But that won’t prevent you to be spammed. If you don’t want to be spammed to have to track IPs and before submitting the email check that the IP didn’t already send an email let’s say in the last 30 seconds, for instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contact form on my website, which actually works :D The problem
I have a contact form, the values of which I'm passing to a php
I have a contact form on site which used to work, but since last
I have a form on my PHP page which performs some ajax validation (that's
I have this PHP contact form which disallows a submission from an existing IP.
I have developed a contact form which incorporates CI form validation, email helper and
i have a PHP contact form that submits data, and an email...: <?php $dbh=mysql_connect
I have a PHP contact form on my website. I have been using it
i have made a simple php contact form following this tutorial: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme The big
I have the following PHP in my contact form. // Ensure a message was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.