Jump to content
xisto Community
Sign in to follow this  
vstxy

How To Make A Form email your self with a form from vistors

Recommended Posts

hey plzzzz all exceperinced pepole in php can you describe step by step how to make a beutifull form in my website to make the visitors email meee plz replay to that step by step coz iam a beginner thx :(

Share this post


Link to post
Share on other sites

It might be easier to use html, or if you really need to use php, just search yahoo or google for "e-mail form php script" I hope that helps.

Share this post


Link to post
Share on other sites

Okay, if you really want to do this yourself, it's really not that hard. First, you need to know basic html forms. The html form has to methods of sending data, post and get. Have you ever noticed a URL that has ?p=7&c=3 or something at the end? Those are get variables, where p == 7 and c ==3. Post will send the information "silently" so it's a little more secure, but if you just want a form that they can use to send you an email, there's no reason to get crazy about security. We'll use the get method, so first we need to start a form in the page you want them to be able to submit from:

<form name="email" method="get" action="mailme.php"></form>

That's doesn't look too bad I hope. The action parameter is the script that will be called when the form is submitted, in this case, mailme.php. Okay, now we need to add a couple of fields. I'm going to just use a subject and body for this script, but you can add others as you see fit.

<form name="email" method="get" action="mailme.php">  Subject: <input type="text" name="subject" size="25"><br>  Body: <textarea name="body" width="40" height="3"></textarea><br>  <input type="submit" value="Send Email" name="submit"></form>

Okay, let's go through this. First, we have the subject. The <input> tag will let us make different types of ways to get information from the user, in this case we make a textbox with the type="text" parameter. Note that we name it "subject" this is important later in the php script. The size just makes it a decent width for what we expect to be put in the field. Next we have the body, here we're using a textarea, this is just like the text box except has multiple lines so they can write and see their message a little easier. Width and height are again just making it about the right size, and name is "body" for this, again this is important later. Notice we have and end tag for the textarea. If we wanted to we could put some text between the tags that would be the default value of the textarea. I chose not to use one for the demo but you may want to. The last item in the form is a submission button. This will actually submit the information to the script defined in the action parameter above. Value is the text in the button, type is submit, which is telling the form, when they click, run the action script. Again, note the name is "submit". Okay, that's all for the html form, save it as email.html or something and include it on one of your pages. Now let's write some php.

$subject = $_GET['subject'];$body = $_GET['body'];$submit = $_GET['submit'];

Remember how I kept telling you those names we set were important, well, here's why. When we want to access a variable passed to a script using the get method, we use the superglobal php variable $_GET. Think of it as an array, that has a element for each piece of data you passed it. The "key" for each piece of data will be the name you set in the form, so we acces the subject field's value with $_GET['subject']. These three lines just make the variables easier to use later in the script. The same thing could of been accomplished with one line of code:

extract($_GET);

I didn't use this because I felt the other way illustrates the get method better but you should always know the easiest way to do things too. Next we want to check if they put anything in, if they didn't we don't want to send ourselves a blank email, and we'll give them another try.

if (!$subject || !$body || !$submit){  printf("Both fields are required!<br>\n");  include("email.html");} else {  mail("jonypawks@gmail.com", $subject, $body, "Website Comment");  printf("Mail sent, thank you!");}

The if line just checks to make sure there's a value in each variable. We check subject and body to make sure that they entered values into the form. We're checking submit so we know that they came from our email.html, otherwise they could just type in the path to the php script. This isn't really a big deal for a script like this, but it's good practice because it can become a security issue if the script is doing something more sensitive. So, if any of those variables have no values we will print a statement telling them both fields are required and then we include the email form so they don't have to hit back. If there is a value for each variable we send the mail. The mail command is really pretty simple here's a description of it:

mail(to, subject, message, headers)

So you can see we're sending to the email address "jonypawks@gmail.com" which is my email address, and then we're setting the subject and message to our subject and body variables respectively. Then we're adding the header information "Website Comment". This is an unnecessary parameter but it can be useful. Have you ever noticed all the extra text at the top of an email that no one reads? Well, that's the header. And whatever we put in that parameter is added to that. There's really no point to this, I'm just showing you what you can do with this function. Finally, we tell them that the mail was sent.

I hope you were able to understand everything well enough. If not, just post a question and I will do my best to answer it. You can see a working copy of this program here. And here's the source code for the two files:

email.html
<html><body><b>Email Me!</b><br><form name="email" method="get" action="mailme.php">  Subject: <input type="text" name="subject" size="25"><br>  Body: <textarea name="body" width="40" height="3"></textarea><br>  <input type="submit" value="Send Email" name="submit"></form></body></html>

mailme.php
<?php$subject = $_GET['subject'];$body = $_GET['body'];$submit = $_GET['submit'];if (!$subject || !$body || !$submit){  printf("Both fields are required!<br>\n");  include("email.html");} else {  mail("jonypawks@gmail.com", $subject, $body, "Website Comment");  printf("Mail sent, thank you!");}?>

Hmm, this turned out to be longer than I expected, maybe I should of put this in the tutorials section. Well, good luck and try not to abuse the page too much ;-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.