Jump to content

Recommended Posts

Hey

 

I was wondering how to make an email contact form in PHP. I have tried to write one in HTML, but I can't do that if I want some functions. This is what I want of functions:

 

A contact form where people on my homepage can write a message to different persons, like:

 

1) Name.

2) Email.

3) A field where people can choose whom to send the message to (like a drop down box).

4) Subject.

5) Message.

6) A submit button where (if the form is filled out correctly) people get an pop-up that the message is send correctly.

 

I am totally new to PHP, so if anybody got a link to a guide, or maybe got a premade code, it would be very nice.

 

Thanks in advance.

 

Sixmax

Link to comment
https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/
Share on other sites

Okay - now i have made the HTML look:

 

<form method="post" action="contact.php">
<table border="0" cellpadding="0" cellspacing="0" align="left">
<tr>
<td><h3 style="border-bottom:1px solid black;">Contact</h3></td>
</tr>
<tr>
<td>
<label for="Name" style="display:block;margin-bottom:5px;">Name:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div>

<label for="Email" style="display:block;margin-bottom:5px;">Email:</label><input type="text" name="Email" id="Email" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div>

<label style="display:block;margin-bottom:5px;" for="Receiver">Receiver:</label><select name="Receiver" id="Receiver"><option value="[email protected]">Person 1</option><option value="[email protected]">Person 2</option><option value="[email protected]">Person 3</option><option value="[email protected]">Person 4</option></select><div style="clear:left;height:20px;"> </div>

<label for="Name" style="display:block;margin-bottom:5px;">Subject:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div>

<label for="Message" style="display:block;margin-bottom:5px;">Message:</label><textarea name="Message" id="Message" style="width:200px;height:200px;"></textarea><div style="clear:left;height:20px;"> </div>

</td>
<tr>
<td align="left">
<input type="submit" value=" Submit "> <input type="reset" value=" Reset ">
</td>
</tr>
</table>
</form>

 

The question is now - how to create the contact.php so that if "Person 2" is chosen at the drop down box, then the mail is sent to the correct email (in this case [email protected] - just example emails).

I have seen a lot of free copy-pasteable forms on the net, but they only handle the possibility of one receiver - not to choose between different receivers.

 

Hope that some PHP freak got the answer :-)

 

Sixmax

You would do an if/else statement:

 

ex.


<?php
$contact1 = "[email protected]";
$contact2 = "[email protected]";
$contact3 = "[email protected]";

if($contact1)
{
mail(to, subject, message)
}
elseif($contact2)
{
mail(to, subject, message)
}
else
{
mail(to, subject, message)//this mails $contact3
}

?>

This is what it would look like for you:

 

<?php

$get_name = $_POST['name'];
$get_email = $_POST['email'];
$get_receiver = $_POST['receiver'];
$get_subject = $_POST['subject'];
$get_message = $_POST['message'];

$to1 = "[email protected]";
$to2 = "[email protected]";
$to3 = "[email protected]";
$to4 = "[email protected]";

$subject = $get_subject;
$message = $get_message;

$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From: $get_email";

if($get_receiver == $to1)
{
mail($to1, $subject, $message, $header)
}
elseif($get_receiver == $to2)
{
mail($to2, $subject, $message, $header)
}
elseif($get_receiver == $to3)
{
mail($to3, $subject, $message, $header)
}
else
{
mail($to4, $subject, $message, $header)

?>

 

Modify for your needs but you should get it from here.

You shouldn't put the email addresses into your form, since they will be screen scraped, instead do something like this:

<form method="post" action="contact.php">
<table border="0" cellpadding="0" cellspacing="0" align="left">
<tr>
<td><h3 style="border-bottom:1px solid black;">Contact</h3></td>
</tr>
<tr>
<td>
<label for="Name" style="display:block;margin-bottom:5px;">Name:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div>

<label for="Email" style="display:block;margin-bottom:5px;">Email:</label><input type="text" name="Email" id="Email" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div>

<label style="display:block;margin-bottom:5px;" for="Receiver">Receiver:</label><select name="Receiver" id="Receiver"><option value="1">Person 1</option><option value="2">Person 2</option><option value="3">Person 3</option><option value="4">Person 4</option></select><div style="clear:left;height:20px;"> </div>

<label for="Name" style="display:block;margin-bottom:5px;">Subject:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div>

<label for="Message" style="display:block;margin-bottom:5px;">Message:</label><textarea name="Message" id="Message" style="width:200px;height:200px;"></textarea><div style="clear:left;height:20px;"> </div>

</td>
<tr>
<td align="left">
<input type="submit" value=" Submit "> <input type="reset" value=" Reset ">
</td>
</tr>
</table>
</form>

and in the processing script:

<?php
$emails = array('','[email protected]','[email protected]','[email protected]','[email protected]');
if ($_POST['Receiver'] > 0 && $_POST['Receiver'] < 5) $to = $emails[$_POST['Receiver']];
else
   die('Bad Receiver recieved: ' . htmlentities($_POST['Receiver']));
//
// rest of your code
//
?>

 

Ken

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.