NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page PHP Form Script, Nice 'n' Easy!

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 04-30-2004, 03:48 PM THREAD STARTER               #1 (permalink)
NamePros Member
 
OneEyedDog's Avatar
Join Date: Mar 2004
Location: Middle Earth (NZ)
Posts: 149
OneEyedDog is on a distinguished road
 



PHP Form Script, Nice 'n' Easy!


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 ) 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:

Quote:

<?

$To = "you@youremail.com";

$From = "Whoever You Want Here <you@youremail.com>";

$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:

Quote:

<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

Quote:

<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>
????: NamePros.com http://www.namepros.com/code/28903-php-form-script-nice-n-easy.html
</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%">&nbsp;</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

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:

Quote:
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=28903

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.

Warm Wishes,
__________________
Script Installation, Avatars, Banners and Buttons for $NP or Paypal, PM me your requirements :D
Deni J Vanin | Freelance Jobs and Forums | Web Design
Last edited by OneEyedDog; 04-30-2004 at 04:06 PM.
OneEyedDog is offline  
Old 05-03-2004, 05:16 PM   #2 (permalink)
Electrifying Guy
 
Dave Zan's Avatar
Join Date: Sep 2003
Posts: 4,749
Dave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond repute
 




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
????: NamePros.com http://www.namepros.com/showthread.php?t=28903
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!
Dave Zan is offline  
Old 05-04-2004, 12:44 AM THREAD STARTER               #3 (permalink)
NamePros Member
 
OneEyedDog's Avatar
Join Date: Mar 2004
Location: Middle Earth (NZ)
Posts: 149
OneEyedDog is on a distinguished road
 



Howdy

Quote:
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:
????: NamePros.com http://www.namepros.com/showthread.php?t=28903

Quote:

<?

$To = "you@youremail.com";

$From = "Whoever You Want Here <you@youremail.com>";

$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.

Quote:
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,
__________________
Script Installation, Avatars, Banners and Buttons for $NP or Paypal, PM me your requirements :D
Deni J Vanin | Freelance Jobs and Forums | Web Design
OneEyedDog is offline  
Old 05-04-2004, 10:26 AM   #4 (permalink)
Electrifying Guy
 
Dave Zan's Avatar
Join Date: Sep 2003
Posts: 4,749
Dave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond reputeDave Zan has a reputation beyond repute
 




I'll look up the MLM I mentioned earlier. Be right back!

Thanks for the post earlier!
Last edited by davezan; 05-04-2004 at 02:01 PM.
Dave Zan is offline  
Old 05-20-2004, 05:48 PM   #5 (permalink)
Account Suspended
 
BoRiCuA's Avatar
Join Date: Apr 2004
Location: Connecticut
Posts: 586
BoRiCuA is just really niceBoRiCuA is just really niceBoRiCuA is just really niceBoRiCuA is just really nice
 



Add this to your forum, it's very useful:

<input type="hidden" name="REMOTE_ADDR" value="<? echo $REMOTE_ADDR;?>">
BoRiCuA is offline  
Old 06-19-2004, 01:37 PM   #6 (permalink)
Melinda
Join Date: Mar 2004
Posts: 1,933
emeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud of
 

Member of the Month
July 2004
Breast Cancer
great post! Keep up the great php work.
Melinda
Last edited by emeri; 06-20-2004 at 01:20 PM.
emeri is offline  
Old 06-19-2004, 07:17 PM THREAD STARTER               #7 (permalink)
NamePros Member
 
OneEyedDog's Avatar
Join Date: Mar 2004
Location: Middle Earth (NZ)
Posts: 149
OneEyedDog is on a distinguished road
 



Thanks
__________________
Script Installation, Avatars, Banners and Buttons for $NP or Paypal, PM me your requirements :D
Deni J Vanin | Freelance Jobs and Forums | Web Design
OneEyedDog is offline  
Old 06-20-2004, 04:09 PM   #8 (permalink)
NamePros Regular
Join Date: Apr 2004
Location: NL
Posts: 808
Slewey is just really niceSlewey is just really niceSlewey is just really niceSlewey is just really nice
 



very nice
might gonna use this

tnx!
Slewey is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 04:09 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger