Search code examples
phpformscontactsmailto

what should I use to create a web contact form?


Hi guys this is my first question on stackoverflow.

I'm an amateur web designer designing a mostly static website..I need to create a contact form for user queries.What's the best approach for this task?

1)php's mailto function?

2)form data stored in a text file or spreadsheet?

3)database-connected(not preferred)

Any help would be really appreciated!!


Solution

  • To create a simple contact us form in php,you dont need to create any database. You have to use mail() function in PHP

    You can refer following links to built simple contact us form :-

    http://www.phpeasystep.com/phptu/8.html

    http://www.freecontactform.com/email_form.php

    OR

    page1.php

    <h2>Your Title</h2>
     
    <form action="receiving.php" method="POST">
     
    Name:<br><input type="text" name="name" size="40" /><br><br>
     
    Email:<br><input type="text" name="email" size="40" /><br><br>
     
    Phone:<br><input type="text" name="phone" size="40"><br><br>
     
    Message:<br><textarea name="message" rows="3" cols="31" > </textarea><br><br>
     
    <input type="submit" name="submit" value="Submit" />
    <br><br>
     
    </form>
    

    receiving.php

    <?php
    
    $name = $_POST['name']; 
        $email = $_POST['email']; 
        $phone = $_POST['phone']; 
        $message = $_POST['message']; 
    
    if(isset($_POST['submit']))
    
    {
        $from_add = "contactform@yourwebsite.com"; 
    
        $to_add = "yourname@yourwebsite.com"; 
    
        $subject = "Your Subject Name";
    
        $message = "Name:$name \n Email: $email \n Phone: $phone \n 
    Message: $message";
    
        $headers = "From: $from_add \r\n";
        $headers .= "Reply-To: $from_add \r\n";
        $headers .= "Return-Path: $from_add\r\n";
        $headers .= "X-Mailer: PHP \r\n";
    
    
        if(mail($to_add,$subject,$message,$headers)) 
        {
            $msg = "Mail sent";
        } 
    }
    
    print "<p> Thank you $name for your message,
        we will be in contact shortly. <a href=\"index.php\">Click here</a>
        to continue </p>" ;
    
    ?>
    

    NOTE :- You cannot send mail from localhost, configure some other smtp at localhost eg : google,yahoo...