Lost Password?


Go Back   CodeCall Programming Forum > Web Development Forum > HTML Programming

HTML Programming Forum discussion covering HTML, XHTML, DHTML and all flavors of HTML. Hypertext Markup Language is used to create websites.

Closed Thread
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-2007, 08:26 PM
priorityone
Guest
 
Posts: n/a
Question A little help

What would be the code for a basic form that asks a persons: name: email address and comments and then sends this info to a designated email recipient?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!

Sponsored Links
  #2 (permalink)  
Old 01-21-2007, 05:13 AM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

PHP Tutorial: Writing Your First PHP Script: A Feedback Form (a FormMail Script) (thesitewizard.com)
Visit that link and there's a complete tutorial on this.

Quote:
HTML Code

<form method="post" action="sendmail.php">
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input type="submit" />
</form>
Quote:
The Feedback Form PHP Script

Now all that remains is to code "sendmail.php". This is made extremely easy by the facilities available in PHP. Type the following code into a file named "sendmail.php". Do not put anything else into that file, ie, don't put in any other HTML tags or headers, etc.

<?
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

mail( "yourname@example.com", "Feedback Form Results",
$message, "From: $email" );
header( "Location: http://www.example.com/thankyou.html" );
?>

When the form is submitted to sendmail.php, the contents of the "email" field in the form is put into a PHP variable called $_REQUEST['email']. Likewise the contents of the "message" field is put into the variable $_REQUEST['message'].

If you had named the fields in your form "emailofsender" and "contentsofmessage", then the information submitted in those fields would have been available to your script in the variables $_REQUEST['emailofsender'] and $_REQUEST['contentsofmessage'] respectively. I think you get the idea.

The first thing we do in our PHP script is to make the information that is submitted easily accessible to the rest of the program.

Firstly, we made a copy of the contents of $_REQUEST['email'] in a variable we call $email. This was done in the line
$email = $_REQUEST['email'] ;

Note that we don't really have to call this new variable $email - we could have called it $thingamajig if we wished, but it makes sense to name a variable with some meaningful name.

Likewise, in the next line, we made a copy (assigned) of $_REQUEST['message'] in a variable $message.
$message = $_REQUEST['message'] ;

Again, we could have named the new variable anything we wanted - but it's easier for us to understand the program if the variable name reflects what it does.

The real workhorse of this script is in the line beginning with "mail".

mail( "yourname@example.com", "Feedback Form Results",
$message, "From: $email" );

mail is a special function in PHP that sends mail. The first parameter to mail is supposed to contain the email address you want the form contents to be sent to, such as your own email address. The second parameter is the "Subject" of the email message. The last two parameters are the content of the message and the headers you want sent, respectively. We want a "From" header so that we know who is sending the email to us and can reply to him/her if we need to.

Notice that, like many other programming languages, strings (sequences of characters) are enclosed in double quotes, such as "Feedback Form Results".

Variables like $message can be used as-is. Note also that you can also interpolate (introduce) the contents of the variable $email into a string, like "From: $email", so that if your $email string contained an address like "william@shakespeare.com", the final string that is passed to the mail function will be "From: william@shakespeare.com".

You can also use single quotes (such as those in 'Hi there') to quote strings, but when you do so, the variables included are not expanded. This is useful if, for some reason, you really want to pass the string 'From: $email' to mail without PHP translating that to "From: william@shakespeare.com".

You can't interpolate variables like $_REQUEST['email'] and $_REQUEST['message'] in the same way, which is why we assigned their contents to $email and $message. The latter are also easier to read anyway.

Finally, it is appropriate to thank the visitor for his message. This is done with the line
header( "Location: http://www.example.com/thankyou.html" );

This line causes PHP to send an HTTP header back to the visitor's browser telling it to load the URL "http://www.example.com/thankyou.html". The "header" function allows us to send any HTTP header to the browser.

You will of course have to create such a file called "thankyou.html" with some sort of message to thank your visitor for his efforts, otherwise your visitor will be greeted with an unfriendly "404/File Not Found" error after he sends his message. You should also replace the URLs and email addresses with the correct ones if you want to use that script on your site.

By the way, the script has to be enclosed within the "<?" and "?>" tags because the PHP processor treats all input as HTML code unless otherwise specified. On some systems, you may need to use "<?php" and "?>" as the opening and closing tags to get the script to work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
  #3 (permalink)  
Old 01-25-2007, 12:10 PM
priorityone
Guest
 
Posts: n/a
Default

Xtraze thanks..If I copy this into the html format this will enable people to fill out a form and the information will be sent to my email? I am very grateful to you..thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
  #4 (permalink)  
Old 01-25-2007, 03:16 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,478
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Or you can use our tutorial mail() function ...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
  #5 (permalink)  
Old 01-25-2007, 03:32 PM
priorityone
Guest
 
Posts: n/a
Default

Would this be suitable: (mail function) for a website that wants just the name email and comments ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!

Sponsored Links
  #6 (permalink)  
Old 01-25-2007, 09:33 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,478
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Yes, a believe that is the simplest thing you can use, except for of course the mailto: function in HTML which doesn't provide you with a form
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
  #7 (permalink)  
Old 01-28-2007, 06:47 AM
priorityone
Guest
 
Posts: n/a
Default

Oh, ok..thanks for all the advice. I will look at both the coding given here and the other info and see which one is going to work best for me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Closed Thread



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

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 07:08 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads