php contact form, with no page redirect?

Associate
Joined
20 May 2007
Posts
441
Hey everyone

This is just a general question i was wondering, with php and sending an e-mail most tutorials have a web page with the form then when submit is pressed it send the user to some other php page where the input can be validated and sent, is it possible to just send the e-mail without having to change pages?

Im new to php so sorry if this is a bit of a stupid question just my googling doesn't seem to be giving me any answers

Cheers
Gaunt
 
Simply, yes :)

All you need is to check if the page has been posted or not, if it has been, then run a check on the POST data and send email if its all valid or present the user with error messages if something isn't right.
If the page hasn't been posted then just show the form :)
 
cheers tsinc

Well i got it up and running, however I found a tiny lil problem, it seems that is I refresh the page after the email has bn sent (i.e. the thank you message is displayed), the refresh will resend the information, is there anyway of sending the email then resetting the post data??
 
Thats to do with the browser resending the data not PHP but you could try this :

Code:
<?php
	if(isset($_POST['submit'])) {
		if(isset($_SESSION['count'])) { 
			$_SESSION['count']++;
		} else {
			$_SESSION['count'] = 1;
		}
		if ($_SESSION['count'] > 1) {
			echo "You have already sent the message";
			exit;
		} else {
			// Send message data.
		}
	}
?>

or if you dont mind the page refreshing/redirecting if they refresh the page then this would work:

Code:
<?
	if(isset($_POST['data']))
	{
   	 	// Handle POST-data.
    		header("location:" . $_SERVER['PHP_SELF']);
	}
?>
 
Last edited:
Thats to do with the browser resending the data not PHP but you could try this :

Code:
<?php
    if(isset($_POST['submit'])) {
        if(isset($_SESSION['count'])) { 
            $_SESSION['count']++;
        } else {
            $_SESSION['count'] = 1;
        }
        if ($_SESSION['count'] > 1) {
            echo "You have already sent the message";
            exit;
        } else {
            // Send message data.
        }
    }
?>
or if you dont mind the page refreshing/redirecting if they refresh the page then this would work:

Code:
<?
    if(isset($_POST['data']))
    {
            // Handle POST-data.
            header("location:" . $_SERVER['PHP_SELF']);
    }
?>

Nice idea, I'll be using that one!

Thanks
 
Back
Top Bottom