Domanda

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!!

È stato utile?

Soluzione

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...

Altri suggerimenti

Sample code of contact form using mail function

<?php
if (isset($_POST['action'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    if ($name == "" || $email == "" || $message == "") {
        echo "All fields are required, please fill the form again.";
    } else {
        $from = "From: $name<$email>\r\nReturn-path: $email";
        $subject = "Message sent using your contact form";
        mail("youremail@yoursite.com", $subject, $message, $from);
        echo "Email sent!";
    }
}
?>

<form  action="" method="POST"> 
    Your name:<br> 
    <input name="name" type="text" /><br> 
    Your email:<br> 
    <input name="email" type="text"/><br> 
    Your message:<br> 
    <textarea name="message" rows="7" cols="30"></textarea><br> 
    <input type="submit" name="action" value="Send email"/> 
</form>

Simply build a html-form and send the data to a php-file. Database is not needed at all, unless you want to store all messages or IP.
A simple search will get you a lot of results.

In the phpfile you have two options: Use php's mail() as is, or try PHPmailer. The latter is a bit more complecated, but ends up less as spam, it sets headers and everything for you.

Especially when you mail from multiple pointer on your website, I recommend PHPmailer.

In reply to one of your comments:
All mailing happends on the server. PHP is a serverside language, all actions take place on the server, not on the users computer. For that reason, your code should always be on a server, or local with WAMP (or LAMP)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top