Jump to content

Recommended Posts

Hi, I have this form that works pretty well apart from the page reloading twice, once to submit and to display the thank you message and once again to reload the form after 10 secs, is it possible to use a bit of ajax code to do this?

 

Here is my code...pls note I am rubbish at coding, if it possible can you pls show me where to place the code...many thnaks

 

<?php
                 $error    = ''; // error message
                 $name     = ''; // sender's name
                 $email    = ''; // sender's email address
			 $referred = ''; // referred
                 $message  = ''; // the message itself
               	 $spamcheck = ''; // Spam check

            if(isset($_POST['send']))
            {
                 $name     = $_POST['name'];
                 $email    = $_POST['email'];
			 $referred  = $_POST['referred'];
                 $message  = $_POST['message'];
               	 $spamcheck = $_POST['spamcheck'];

			 $SpamErrorMessage = '<div class="errormsg">No website URLs permitted, click <a href="index.php">here</a> to go back</div>';


			if (preg_match("/http/i", "$name")) {echo "$SpamErrorMessage"; exit();} 
			if (preg_match("/http/i", "$email")) {echo "$SpamErrorMessage"; exit();}  
			if (preg_match("/http/i", "$message")) {echo "$SpamErrorMessage"; exit();} 
			if (preg_match("/http/i", "$spamcheck")) {echo "$SpamErrorMessage"; exit();} 
                if(trim($name) == '')
                {
                    $error = '<div class="errormsg">Please enter your name.</div>';
                }
            	    else if(trim($email) == '')
                {
                    $error = '<div class="errormsg">Please enter your E-mail?</div>';
                }
                else if(!isEmail($email))
                {
                    $error = '<div class="errormsg">Incorrect Email format.!</div>';
                }
				else if(trim($message) == '')
                {

                    $error = '<div class="errormsg">Message please!</div>';
                }
			else if(trim($referred) == '')
                {
                 $error = '<div class="errormsg">Error: Please tell us how you found us.</div>';
             
                }

          	else if(trim($spamcheck) == '')
            {
            	$error = '<div class="errormsg">Please enter: Spam Check!</div>';
            }
          	else if(trim($spamcheck) != '5')
            {
            	$error = '<div class="errormsg">Spam Check: Number incorrect! 2 + 3 = ???</div>';
            }
                if($error == '')
                {
                    if(get_magic_quotes_gpc())
                    {
                        $message = stripslashes($message);
                    }
                    // the email will be sent here
                    // make sure to change this to be your e-mail
                    $to      = "email here";
                    // the email subject
                    // '[Contact Form] :' will appear automatically in the subject.
                    // You can change it as you want
                    $subject = 'subject here' . $subject;
                    // the mail message ( add any additional information if you want )
                    $msg     = "From : $name \r\ne-Mail : $email  \r\nReferred By: $referred \r\n\n"  . "Message : \r\n$message";
                    mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

	   ?>
                  <!-- Message sent! (change the text below as you wish)-->
                  <div style="text-align:left;">                  
                       <p>Thank you <b><?=$name;?></b>,<br/> we will be in touch.</p>
                  </div>
                  <!--End Message Sent-->
            <?php				


echo 'You\'ll be redirected in <span id="countdown"></span> secs. <br/>If not, click <a href="index.php">here</a>.';
?>

<script type="text/javascript">

$(function(){
  var count = 10;
  countdown = setInterval(function(){
    $('#countdown').html(count + " ");
    if (count == 0) {
      window.location.href = window.location;
    }
    count--;
  }, 1000);

});

</script>

<?php					

$replymessage = "Hello $name
We have recieved your message, we will be in touch within 24hrs.
Regards

--------------------------------------------------
Your Message:
$message
--------------------------------------------------
Thank you. ";
$message = "name: $name \nQuery: $message";
$replyemail = "email here";
mail("$replyemail",
     "$emessage",
     "From: $email\nReply-To: $email");
mail("$email",
     "Message: $subject",
     "$replymessage",
     "From: $replyemail\nReply-To: $replyemail");
echo $success_sent_msg;
                }
            }
            if(!isset($_POST['send']) || $error != '')
            {
            ?>
            <!--Error Message-->
            <?=$error;?>
            <form  method="post" name="contFrm" id="contFrm" action="">
                      <label><span class="required">*</span> Full Name:</label>
            			<input name="name" type="text" class="box" id="name" size="27" value="<?=$name;?>" />
            			<label><span class="required">*</span> Email: </label>
            			<input name="email" type="text" class="box" id="email" size="27" value="<?=$email;?>" />
					<label><span class="required">*</span> Referred by:</label>
					<select name="referred" value="<?=$referred;?>">
									<option value="Choose" selected>Please select..</option>
									<option value="Google" >Google</option>
									<option value="Gumtree" >Gumtree</option>
									<option value="Yell.com" >Yell.com</option>
									<option value="Flyer">Flyer/Business card</option>
									<option value="Friend">Friend</option>
									<option value="other">Other</option></select>
                 		<label><span class="required">*</span> Message: </label>
                 		<textarea name="message" cols="25" rows="3"  id="message"><?=$message;?></textarea><br /><br />
            			<label><span class="required">*</span> Spam Check: <b>2 + 3=</b></label>
					<input name="spamcheck" type="text" class="box" id="spamcheck" size="4" value="<?=$spamcheck;?>" />
            			<!-- Submit Button-->
                 		<input name="send" type="submit" value="Submit" alt="Submit" class="button" id="send" />
            </form>
		<!-- E-mail verification. Do not edit -->
            <?php
            }
            function isEmail($email)
            {
                return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
                        ,$email));
            }
            ?>
		<br />
     
     

 

Many Thanks for you help!

Link to comment
https://forums.phpfreaks.com/topic/256153-display-thank-you-after-submitting-form/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.