Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

php simple contact form

hi there

i need simple contact form php codes for my cilent website? Can someone help me?

The php track goes over that in depth. I highly recommend you take and follow that track. But if you are in a rush.... there is a section in there directly related to what you are asking.

Also its not good practice to ask for code without providing your own.

2 Answers

<form action="" method="POST"><table style=" ;" border="0" cellspacing="18" cellpadding="10" id="kontaktform">
<tbody>
<tr>
<td><input name="name"  onfocus="if (this.value=='Name') this.value = ''" onblur="if (this.value=='') this.value = 'Name'" type="text" value="Name" /></td>
<td><input name="lastname" onfocus="if (this.value=='Lastname') this.value = ''" onblur="if (this.value=='') this.value = 'Lastname'" type="text" value="Lastname" /></td>
</tr>
<tr>
<td><input name="phone" onfocus="if (this.value=='Phone') this.value = ''" onblur="if (this.value=='') this.value = 'Phone'" type="text" value="Phone" /></td>
<td><input name="address" onfocus="if (this.value=='E-mail') this.value = ''" onblur="if (this.value=='') this.value = 'E-mail'" type="text" value="E-mail" /><input name="email" style="display:none" type="text" value="E-mail" /></td>
</tr>
<tr>
<td colspan="2" align="right"><textarea name="text" onfocus="if (this.value=='Message') this.value = ''" onblur="if (this.value=='') this.value = 'Message'" type="text" >Message</textarea ><br /><input name="submit"   type="submit" value="Send" /></td>
</tr>
</tbody>
</table></form>
<?php
$to = "example@example.com";
$subject = "Contact from webpage!";

//name, lastname, phone, email
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$emails = $_POST['address'];
$emailcheck = $_POST['email'];
$text = $_POST['text'];



if($name != '' & $lastname != '' & $phone != '' & $emails != '' & $text != '' & $name != 'Name' & $lastname != 'Lastname' & $phone != 'Phone' & $emails != 'E-mail' & $text != 'Message' & $emailcheck == 'E-mail'  ){

 $body = "Hi,\n\nYou have new message from $name $lastname:\n\n$text\n\nPhone: $phone \n E-mail: $emails";
 if (mail($to, $subject, $body, "from: $emails")) {
   echo("<h3>Thanks!</h3><center style='font-size:14px;'>Your message successfully sent</center>");
  } else {
   echo("<p>Email delivery failed…</p>");
  }
}
?>```

that is really good.

i got this codes here

'''<form name="contactform" method="post" action="contact.php">

<label for="first_name">First Name *</label>

<input type="text" name="first_name" maxlength="50" size="30">

<label for="last_name">Last Name *</label>

<input type="text" name="last_name" maxlength="50" size="30">

<label for="email">Email Address *</label>

<input type="text" name="email" maxlength="80" size="30">

<label for="telephone">Telephone Number</label>

<input type="text" name="telephone" maxlength="30" size="30">

<label for="comments">Comments *</label>

<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>

<br>

    <input id="submit" name="submit" type="submit" value="Submit">

</form>'''

I want add Anti-spam protection to this, is that possible?