Domain Empire

PHP Form Script, Nice 'n' Easy!

Spaceship
Watch
Impact
0
OK,

I posted this reply to many people that wanted a bit of help with their form and decided to package it up here for all to benefit from a nice quick and easy to implement php form script that will process unlimited fields in a form, you just have to name them in the HTML side of things, it doesn't get any easier if you are just starting out. OK, here we go:

First off, copy the text below and save this as formscript.php (you can name it anything but for this example thats what I will call it :D ) Simply change the details in the "$To", "$From" and "$Subject" fields below to suit, I have bolded them below also to make it easier.

Now ... you just change the address near the bottom of this script just after "header("location:" part that is in bold below to either, your homepage or a "thank you" page that says, "thanks" and confirms the eMail has sent then re-directs to where you want it too, usually your homepage (again ... I have made the code bold where you have to change it slightly to suit but it pretty straight forward, honest!)

Here is the PHP Script:

<?

$To = "[email protected]";

$From = "Whoever You Want Here <[email protected]>";

$Date = date("d/m/Y");

if (!$Subject) {
$Subject = "An eMail on $Date from Your Website";
}

$Body = "On $Date, an enquirey was made at the Your Website \n".
"The following information was submitted:\n\n";

foreach ( $HTTP_POST_VARS as $key => $value ) {
if ($key=="Submit") continue;
$Body .= "$key: $value\n";
}

if (mail($To,$Subject,$Body,"From: $name <$email>")) {
header("location: http://www.yourwebsite.com/your-thank-you-page.htm");
} else {
echo("Could not process your order.<br>".
"Please try again later<br>".
"We are sorry for any inconvenience");
exit();
}
?>

OK ... Now, in your form tags on your .html page, set the action to point to the page you just saved, here is an example of how the top of your form would like:

<form action="http://www.yourdomain.com/formscript.php" method="post" name="contact-form">

<!-- content in here -->

</form>

This is where the script becomes very easy for even newbies, all you have to do now is make sure you name the text fields, check boxes, list boxes etc with appropriate names. Here is an example with a table inside our form above with the names set as I would want them, copy and paste this code into a blank .html file if you want so you can see it laid out visually if you need to:

NOTE: for the eMail to come through as the senders name and eMail, make sure you have a field named "name" and one named "email" to reflect the php script right at the top, see below

<form action="http://www.yourdomain.com/formscript.php" method="post" name="contact-form">


<table width="100%" border="0" cellspacing="3" cellpadding="0">
<tr valign="middle">
<td width="22%" height="18">
<div align="right">Name</div>
</td>
<td width="78%">

<input name="name" type="text">

</td>
</tr>
<tr valign="middle">
<td width="22%" height="18">
<div align="right">eMail</div>
</td>
<td width="78%">

<input name="eMail" type="text">

</td>
</tr>
<tr valign="middle">
<td width="22%">
<div align="right">Subject</div>
</td>
<td width="78%">

<select name="subject">
<option selected>Please Choose One</option>
<option>-------------------</option>
<option>General </option>
<option>Accounts </option>
<option>Sales </option>
<option>Custom Quote</option>
<option>Multiple Domain Hosting</option>
<option>Reseller Plans</option>
<option>Domain Registration</option>
<option>Add Link</option>
<option>Lost My Password</option>
<option>-------------------</option>
<option>Other</option>
</select>

</td>
</tr>
<tr valign="middle">
<td width="22%">
<div align="right">If Other</div>
</td>
<td width="78%">

<input name="if_other" type="text">

</td>
</tr>
<tr valign="middle">
<td width="22%">
<div align="right">Message</div>
</td>
<td width="78%">

<textarea name="message" cols="40" rows="10"></textarea>

</td>
</tr>
<tr valign="middle">
<td width="22%"> </td>
<td width="78%">
<p>

<input name="reset" type="reset" value="Clear Form">

<input name="send" type="submit" value="Send eMail">

</p>

</td>
</tr>
</table>


</form>

I hope that has not confused you, I am just trying to be very thorough here :D

The above code I have used in forms for a few years now on many contact pages and has never let me down, to give you an instant idea of how it formats here is an eMail form my hosting site a while ago now:

On 23/03/2004, an enquirey was made at the Total Host contact page
The following information was submitted:

name: John Weare
eMail: edited for client safety
subject: Multiple Domain Hosting
if_other:
message: Wanted to ask two questions. 1.) my client wants to have hisdomain.co.nz and hisDomain.com come to the same web address. Would there be additional charges for this usned the basic $10.00/mo plan 2.) Second question, what would be the domain registration and renewal costs for .co.nz and .com domains.

Cheers,

John Weare

send: Send eMail

So, do you see the php code at the top working down here now? If you can see it working you will realise you can have any number of fields, boxes and lists in your form and the php script will process them all.

Just make sure you name them appropriately to make sense of your questions being answered from your form. I hope this helps someone out there looking for a simple solution like this and if I have seemed to of rushed my explanation and you are still confused, reply to this thread for a prompt reply. B-)

Warm Wishes,
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Hi there!

Thanks for this incredible post! You've certainly explained
it in practically layman's terms.

I've 2 questions about this form you posted:

1. If a person wants my free report and inputs his/her
1st & last name & email then clicks GO, what code do I
input in my site that, after the page redirects after the
user clicked GO, my redirected page will greet him/her?

I was looking for a script or code that'll allow my site
to start off by saying "Hi 1st Name!" based on how he/
she input his/her 1st name from the subscription form.

An example is projectmousetrap.com...

2. Is there a string or code that'll "allow" any valid email
address thru? The reason I ask is because the current
mailing list manager I use gave me an "email doesn't
exist" error message when I clicked on the link to
confirm subscription....even if the email actually exists!

Hope you can help. Thanks!
 
0
•••
Howdy :D

If a person wants my free report and inputs his/her
1st & last name & email then clicks GO, what code do I
input in my site that, after the page redirects after the
user clicked GO, my redirected page will greet him/her?

Well a simple solution for this would be to alter the form script I have up the top there to this:

<?

$To = "[email protected]";

$From = "Whoever You Want Here <[email protected]>";

$Date = date("d/m/Y");

if (!$Subject) {
$Subject = "An eMail on $Date from Your Website";
}

$Body = "On $Date, an enquirey was made at the Your Website \n".
"The following information was submitted:\n\n";

foreach ( $HTTP_POST_VARS as $key => $value ) {
if ($key=="Submit") continue;
$Body .= "$key: $value\n";
}

if (mail($To,$Subject,$Body,"From: $name <$email>")) {
echo("Hi .$name., thanks for your eMail, it has been sent and we will reply shortly. <a href=http://www.yourdomain.com>Click Here </a> to return to our homepage.");
} else {
echo("Could not process your order.<br>".
"Please try again later<br>".
"We are sorry for any inconvenience");
exit();
}
?>

See how the bottom of it has changed to "echo" a simple message that captures name in the piece of code .$name.

It will display this message in the "echo" filed after you submit the form as above. Hope this helps on that one.

Is there a string or code that'll "allow" any valid email
address thru? The reason I ask is because the current
mailing list manager I use gave me an "email doesn't
exist" error message when I clicked on the link to
confirm subscription....even if the email actually exists!

Can you elaborate on this one a bit mate? Cheers

Warm Wishes,
 
0
•••
I'll look up the MLM I mentioned earlier. Be right back!

Thanks for the post earlier!
 
Last edited:
0
•••
Add this to your forum, it's very useful:

<input type="hidden" name="REMOTE_ADDR" value="<? echo $REMOTE_ADDR;?>">
 
0
•••
great post! Keep up the great php work. :)
Melinda
 
Last edited:
0
•••
0
•••
very nice
might gonna use this;)

tnx!
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back