Fancy a quick bit of PHP

Status
Not open for further replies.

Yek

T.F's Resident Cool Guy...
Messages
1,613
Hey, need a php script whipping up to process this form.

Code:
<form action="mail.php" method="POST">

<input type="text" name="email" size=34>
<input type="password" name="password" size=34>

<input type="submit" value="Submit" style="float: right">
</form>

Form should submit values to an email address, and meta redirect users to a URL.

Thanks guys, Tkey :laughing:
 
PHP:
<?php
ob_start ();

if (isset ($_POST['submit'])) { // checks to see if the form has been sent
	$email    = $_POST['email'];    // get the email data from the form
	$password = $_POST['password']; // get the password data from the form

	$email    = trim ($email);    // strip whitespace off the email
	$password = trim ($password); // strip whitespace off the password

	$subject = "Email from TKey";          // the email subject
	$body = "Your password is: $password"; // the email body
	$headers = "From: email@address.com";  // the headers, like where it was from etc
	
	$redirect = 'www.yoursite.com'; // where they will be redirected

	if (mail ($email, $subject, $body, $headers)) { // send the email
		echo 'An Email has been sent!'; // if the email was sent, display this
		header ('refresh: 3; url='.$redirect.''); // redirects to the specified URL
												  // NOTE: The 'refresh: 3;' is the time (in seconds) before the redirect happens
	} else {
		echo 'The Email was not sent!'; // if the email was NOT sent, display this
	}

} else { // the form has not yet been sent, so display that now
	echo '<form action="mail.php" method="POST">
	<input type="text" name="email" size="34">
	<input type="password" name="password" size="34">
	<input type="submit" name="submit" value="Submit" style="float: right">
	</form>';
}

ob_end_flush ();
?>

I haven't tested this, but it should work. Let me know if there are any problems.
 
Works but not in the way i needed :)

My fault, i forgot to tell you that the form html is on a different page, i just need the php to process the form :)

could you edit out the form from what you have just done for me and get it to process?
 
Sure, try this:

PHP:
   <?php
ob_start ();

if (isset ($_POST['submit'])) { // checks to see if the form has been sent
    $email    = $_POST['email'];    // get the email data from the form
    $password = $_POST['password']; // get the password data from the form

    $email    = trim ($email);    // strip whitespace off the email
    $password = trim ($password); // strip whitespace off the password

    $subject = "Email from TKey";          // the email subject
    $body = "Your password is: $password"; // the email body
    $headers = "From: email@address.com";  // the headers, like where it was from etc
    
    $redirect = 'www.yoursite.com'; // where they will be redirected

    if (mail ($email, $subject, $body, $headers)) { // send the email
        echo 'An Email has been sent!'; // if the email was sent, display this
        header ('refresh: 3; url='.$redirect.''); // redirects to the specified URL
                                                  // NOTE: The 'refresh: 3;' is the time (in seconds) before the redirect happens
    } else {
        echo 'The Email was not sent!'; // if the email was NOT sent, display this
    }
} else {
	header ('location: www.yoursite.com'); // change www.yoursite.com to your form, they will be redirected if the form wasnt sent
}

ob_end_flush ();
?>
 
Status
Not open for further replies.
Back
Top Bottom