Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top