1 reply [Last post]
suavedesign
suavedesign's picture
Offline
Enthusiast
Last seen: 5 years 42 weeks ago
Timezone: GMT-5
Joined: 2010-12-01
Posts: 128
Points: 191

I have a contact form on my site. Once the user presses "submit", he is redirected to a page with a message. I want, instead for the user to stay on that contact page on my website, and have the message appear right there on the page. How do I do that? I dont know javascript/php, I copied the code fot the form from a tutorial.
The url to my site is: http://rejuvenateusa.org/contactr.html

this is the code:

<?php
//Don't want to get your hands dirty? We've placed all variables in a convenient place for you to modify.
$recipientEmail = "[email protected]"; // enter recipient email address. Don't delete the double quotation marks
$emailSubject = "Message from contact form";
$successMessage = "Thanks for your message! We'll be in contact shortly.";
 
$error  = ""; //leave blank
if(isset($_POST['submit'])){
    $name   = $_POST['name'];
    $email   = $_POST['email'];
    $message   = $_POST['message'];
	$result = checkForSpam(Array($name));
	if(trim($name) == "" || trim($email) == "" || trim($message) == ""){
    $error = "Oops! All fields are required.";
	}
	else if($result){
		$error = 'You have entered invalid characters.';
	}
	else if(!validEmail($email)){
		$error = '*Your email address is not valid';
		$email = '';
	} 
	if($error == ''){
	$priority = 3;
	$php_version = phpversion();
	$headers = "From: $name <$email>\n";
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
	$headers .= "X-Priority: $priority\n";
	$headers .= "X-Mailer: PHP $php_version\n";
	$subject = $emailSubject;
	$to_email = $recipientEmail;
	mail($to_email, $subject, $message, $headers);
 
echo $successMessage;
	}
}
if(!isset($_POST['submit']) || $error != '')
{
echo $error;
?>
<form method="post" action="contact.php">
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="text" name="email" value="<?php echo $email; ?>">
<textarea name="message"><?php echo $message; ?></textarea>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
//utility functions
function validEmail($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));
}
function checkForSpam($fields){
    $spam = false;
    for ($i=0;$i<count($fields);$i++){
        if (eregi("%0A",$fields[$i]) || eregi("%0D",$fields[$i]) || eregi("\r",$fields[$i]) || eregi("\n",$fields[$i])){
            $spam = true;
        }
    }
    return $spam;
}
?>

thanks in advance

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 5 weeks 5 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

Hi suavedesign, This is a PHP

Hi suavedesign,
This is a PHP question.
You will need to change you contact form page from html to PHP.
and have the form submit to itself something like:

<form method="post"  action="<?php $_SERVER['PHP_SELF'];?>" >