-2

I don't know much about PHP coding. I have this contact form:

http://jsfiddle.net/HtU4D/

<div id="content">
   <h1> Contact me </h1>

    <form action=" " method="post"  autocomplete="on">
        <p> <label for="username" class="iconic user" > Name <span class="required">*</span></label> <input type="text" name="username" id="username"  required="required" placeholder="Hi friend, how may I call you ?"  /> </p>

        <p> <label for="usermail" class="iconic mail-alt"> E-mail address <span class="required">*</span></label> <input type="email" name="usermail" id="usermail" placeholder="I promise I hate spam as much as you do" required="required"  /> </p>

        <p> <label for="usersite" class="iconic link"> Website </label> <input type="url" name="usersite" id="usersite"  placeholder="eg: http://www.miste.com" /> </p>

        <p> <label for="subject" class="iconic quote-alt"> Subject </label> <input type="text" name="subject" id="subject"  placeholder="What would you like to talk about?" /> </p>

        <p> <label for="message" class="iconic comment"> Message  <span class="required">*</span></label> <textarea placeholder="Don't be shy, live me a friendly message and I'll answer as soon as possible "  required="required" ></textarea> </p>
        <p class="indication"> All fields with a <span class="required">*</span> are required</p>

        <input type="submit" value=" ★  Send the mail !" />     

    </form>     
</div> 

I want the " Send the mail !" button to send the details directly to my email.

What files should I make? The contact form file is already a PHP file but I don't know what to add !

Sorry for my bad English, Let me know if you didn't understand my Question!

Mo.
  • 26,306
  • 36
  • 159
  • 225
user3641208
  • 1,003
  • 1
  • 8
  • 5
  • 1
    Put in short - on server-side (in PHP for example) you have to get the values from the `form`, sanitize them, and use them in the `mail()` function to send the email. But your question is too broad for StackOverflow, there are plenty of sources and tutorials on Internet on how-to. – Bud Damyanov May 15 '14 at 14:26
  • 2
    You'll probably want to use the `PHP` `mail()` function - http://php.net/manual/en/function.mail.php. And your `form action=" "` should point to a `.php` file that will grab the `POST` values from the form. – Nick R May 15 '14 at 14:26
  • You need a script on the server (something like FormMail see http://www.scriptarchive.com/formmail.html) to process the data received from the form. – Marc Audet May 15 '14 at 14:27
  • Anticipating: in my opinion, there is anything wrong with this question (+1 from me). It is polite, and it has fiddle. It can be a possible duplicate, but there is no reason to downvote. – Jacek Kowalewski May 15 '14 at 14:27
  • 1
    @Jacek — The downvote arrow is marked "The question does not show any research". There's no sign that the OP has bothered to learn the first thing about PHP. – Quentin May 15 '14 at 14:30
  • OK. You are right in that case. Thx for the information, and after a while I think this is true, it does not... – Jacek Kowalewski May 15 '14 at 14:31

1 Answers1

5

Create a PHP file like mail.php. In the form you will direct to that file.

<form action="mail.php" method="post">

In the mail.php you will verify the inputs:

$username = isset($_POST["username"]) ? $_POST["username"] : "";

It means that if username is actually set then $username will get that value, the empty string otherwise.

Using the ternary operator. You should do this for all the inputs.

If you don't want to use the ternary operator (which I highly recommend though) then you can do this instead:

if(isset($_POST["username"]))
{
    $username = $_POST["username"];
}
else
{
    $username = "";
}

If you're satisfied with all the inputs then you can use the built in mail function to send the email.

Once completed you can redirect the user back:

header("Location: index.php");

I've answered a similar question before which you can feel free to look at, there are also plenty of resources on the internet that will help you.

Community
  • 1
  • 1
Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • 1
    -1 for using the ternary operator. Altough correct it isn't a nice coding style. And especially difficult for a beginner. – Daan May 15 '14 at 14:29
  • What, -1 for using the ternary operator? Explain yourself, how is if bool then stuff else stuff better? IT's a mess for many variables. – Jonast92 May 15 '14 at 14:30
  • I said that is `especially` difficult for a beginner start with `if else` or something – Daan May 15 '14 at 14:31
  • 1
    I gave a link so OP could look at it, I'm teaching him a better way to do something and you down vote me, that's just wrong. – Jonast92 May 15 '14 at 14:32
  • I don't why people are giving minus vote – Mo. May 15 '14 at 14:32
  • Be honest did you learn the ternary before if else? Atleast add if else too you'll get an upvote – Daan May 15 '14 at 14:32
  • 1
    I disagree with up or down voting over an opinion to use one or the other. Even if OP would be a beginner. It is still a good / not a good answer if you use one or the other. – putvande May 15 '14 at 14:34
  • Thank you, and true I did use if else at first but I thought providing the link would be enough, now nothing should go wrong. – Jonast92 May 15 '14 at 14:34