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 1033395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:13:35+00:00 2026-05-16T14:13:35+00:00

Arrg. I can’t spot the endless loop in this contact form. The thanks div

  • 0

Arrg.

I can’t spot the endless loop in this contact form. The “thanks” div endlessly prints after the form submission. What am I missing? Is the error php or also jQuery? (In the working file, the JS is included via <script type="text/javascript" src="contact.js"></script>)

Thanks

<?php 
if(isset($_POST['submitted'])) {
if(trim($_POST['checking']) !== '') {
$captchaError = true;
} else {

if(trim($_POST['email']) === '')  {
$emailError = 'Please enter a valid email address';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = 'That\'s not a valid email address';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
}

if(!isset($hasError)) {

$emailTo = 'to email';
$subject = 'site email';
$body = "$email";
$headers = 'From: webmail';
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?>


<script type="text/javascript">

$(document).ready(function() {
    $('form#contactForm').submit(function() {
        $('form#contactForm .error').remove();
        var hasError = false;
        $('.requiredField').each(function() {
            if($(this).hasClass('email')) {
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if(!emailReg.test(jQuery.trim($(this).val()))) {
                    var labelText = $(this).prev('label').text();
                    $(this).parent().append('<span class="error">&nbsp;&nbsp;<i>invalid email</i></span>');

                    hasError = true;
                }
            }
        });
        if(!hasError) {
            $('#thanks').fadeOut('normal', function() {
$(this).parent().append('<img src="../loading-small.gif" alt="Loading&hellip;" height="31" width="31" />');

            });
            var formInput = $(this).serialize();
            $.post($(this).attr('action'),formInput, function(data){
                $('form#contactForm').slideUp("fast", function() {                 
                    $(this).before('<p class="thanks">&nbsp;<strong>Thanks!</strong>');
                    $(".thanks").delay(3000).fadeOut();
                });
            });
        }

        return false;

    });
});

</script>


<?php if(isset($emailSent) && $emailSent == true) { ?>

<div class="thanks"></div>

<?php } else { ?>

<form action="http://mysite.com" id="contactForm" method="post">

Sign up for email notification <input type="text" name="email" size="30" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" />

<?php if($emailError != '') { ?><span class="error"><?=$emailError;?></span><?php } ?>

<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit">Send</form>

<?php } ?>
  • 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-05-16T14:13:35+00:00Added an answer on May 16, 2026 at 2:13 pm

    I made some changes to your code and got it to work on my local machine see if this one works for you

     <?php 
     if(isset($_POST['submitted'])) {
        if(trim($_POST['checking']) !== '') {
            $captchaError = true;
        }else{
            if(trim($_POST['email']) === '')  {
                $emailError = 'Please enter a valid email address';
                $hasError = true;
            } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
                $emailError = 'That\'s not a valid email address';
                $hasError = true;
            } else {
                $email = trim($_POST['email']);
            }
        }
    
        if(!isset($hasError)) {
            $emailTo = 'to email';
            $subject = 'site email';
            $body = "$email";
            $headers = 'From: webmail';
            mail($emailTo, $subject, $body, $headers);
            $emailSent = true;
        }
     } 
     ?>
     <html>
        <head>
            <title></title>
            <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
            <script type="text/javascript" src="js/jquery-ui.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('form#contactForm').submit(function() {
                    $('.error').remove(); //remove the existing error messages before we submit
                    var hasError = false;
                    //lets go through all the required feilds
                    $('.requiredField').each(function() {
                        //check to see if we are processing email
                        if($(this).hasClass('email')) {
                            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                            if(!emailReg.test(jQuery.trim($(this).val()))) {
                               // var labelText = $(this).prev('label').text(); //this line seem useless
                                $(this).parent().append('<span class="error">&nbsp;&nbsp;<i>invalid email</i></span>'); // supply eror message
                                hasError = true; 
                            }
                        }
                    });
                    if(!hasError) {
                        $('#thanks').fadeOut('normal', function() {
                            $(this).parent().append('<img src="../loading-small.gif" alt="Loading&hellip;" height="31" width="31" />');
                        });
                        var formInput = $(this).serialize();
                        $.post($(this).attr('action'),formInput, function(data){
                            $('form#contactForm').slideUp("fast", function() {                 
                                $(this).before('<p class="thanks">&nbsp;<strong>Thanks!</strong>');
                                $(".thanks").delay(3000).fadeOut();
                            });
                        });
                    }
                    return false;
                });
            });
    
        </script>
     </head>
     <body>
    
    
    
     <?php if(isset($emailSent) && $emailSent == true) { ?>
    
     <div class="thanks"></div>
    
     <?php } else { ?>
    
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactForm" method="post">
    
     Sign up for email notification <input type="text" name="email" size="30" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" />
    
     <?php if($emailError != '') { ?><span class="error"><?=$emailError;?></span><?php } ?>
    
     <input type="hidden" name="submitted" id="submitted" value="true" />
     <button type="submit">Send</form>
    
     <?php } ?>
        </body>
     </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alrighty, I'm getting quite frustrated, namely because I thought I had this issue solved,
Arrg! My site (in progress) is working great so far in all browsers I've

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.