/    Sign up×
Community /Pin to ProfileBookmark

Email Form Issues [PHP Newbie]

Hello Everybody! I’m new to this forum and to the world of PHP. I have been given a project to create a web contact form with a dropdown box of recipients. I’ve created the html code and the PHP code; however, it doesn’t work. Also, PHP was just installed on my web server, so I’m not sure if it’s my code or server issues. As you can tell from my code, I’m more of a designer than a programmer. :/ ANY help would be GREATLY appreciated!

Thank you!

My HTML CODE:

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Sales Contact Form</title>
<style type=”text/css”>
.container {
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: bolder;
width: 500px;
color: #000;

}
</style>
</head>

<body>
<div class=”container”>
<fieldset>
<legend>Contact Us</legend>
<form action=”contactFormSite/contact_us.php” method=”post” name=”contact” id=”contact_form”>

<table width=”498″ height=”362″ border=”0″ cellpadding=”9″>
<tr>
<td><label>Name</label></td>
<td><input name=”email_address” type=”text” class=”name” size=”40″/></td>
</tr>
<tr>
<td>
<label>Email Address</label></td>
<td><input name=”email_address” type=”text” class=”required email” size=”40″/></td>
</tr>
<tr>
<td> <label>Subject</label></td>
<td><select name=”subject”>
<option value=”Select one:” selected=”selected”>Select one:</option>
<option value=”recipient_5″>Cars</option>
<option value=”recipient_2″>Real Estate</option>
<option value=”recipient_4″>Equipment</option>
<option value=”recipient_3″>Events</option>
<option value=”Recipient_6″>Finance or Accounting </option>
<option value=”Recipient_7″>Website Related Matters</option>
<option value=”recipient_1″>Sales Manager</option>
</select></td>
</tr>
<tr>
<td align=”left” valign=”top”><label>Message</label></td>
<td><textarea name=”message” cols=”40″ rows=”15″ ></textarea></td>
</tr>
<tr>
<td>*</td>
<td><input type=”submit” value=”Submit Form” style=”font-family: ‘Arial’; font-size: 12px; letter-spacing: 1px” />

<input type=”reset” value=”Reset Form” style=”font-family: ‘Arial’; font-size: 12px; letter-spacing: 1px” /></td>
</tr>
</table>

</form>
</fieldset>
</div>

</body>
</html>[/CODE]

My PHP Code:

[CODE]<?php
$state = 1;
if(isset($_POST[‘name’])) {
// multiple recipients
$to = ”; // note the comma
// subject
$subject = $_POST[‘subject’];
// message
$message = ”
<html>
<head>
<title>”.$_POST[‘subject’].”</title>
</head>
<body>
<p>
Name: “.$_POST[‘name’].” <br />

Contact Number: “.$_POST[‘contact_number’].” <br />

Email: “.$_POST[’email_address’].” <br />

Enquiry: “.$_POST[‘message’].” <br />
</p>
</body>
</html>
“;
// To send HTML mail, the Content-type header must be set
$headers = ‘MIME-Version: 1.0’ . “rn”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “rn”;
// Additional headers
$headers .= ‘To: recepient_1 <‘.$to.’>’ . “rn”;
$headers .= ‘From: recepient_1 <‘.$to.’>’ . “rn”;

$to = $recepient = [$_REQUEST[‘recepient’]];

‘recepient_1’ => ‘[email protected]’,
‘recepient_2’ => ‘[email protected]’,
‘recepient_3’ => ‘[email protected]’,
‘recepient_4’ => ‘[email protected]’,
‘recepient_5’ => ‘[email protected]’,
‘recepient_6’ => ‘[email protected]’,
‘recepient_7’ => ‘[email protected]’,
‘recepient_8’ => ‘[email protected]

);

$my_email = $recepient[$_REQUEST[‘recepient’]];

// Mail it
mail($to, $subject, $message, $headers);
$state = 2;

}
?>[/CODE]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 13.2014 — No guarantees this fixes everything (or anything):
[code=php]
$recipient = array(
'recepient_1' => '[email protected]',

'recepient_2' => '[email protected]',
'recepient_3' => '[email protected]',
'recepient_4' => '[email protected]',
'recepient_5' => '[email protected]',
'recepient_6' => '[email protected]',
'recepient_7' => '[email protected]',
'recepient_8' => '[email protected]'
);
if(!isset($recipient[$subject])) {
// handle invalid subject here
}
else {
// Mail it
mail($recipient[$subject], $subject, $message, $headers);
$state = 2;
}
[/code]


However, I just realized your emails will be going out with subjects like "recipient_1", which I'm guessing is not what you really want? ?
Copy linkTweet thisAlerts:
@thefriz55authorMar 13.2014 — Thank you for the reply! I put your suggested code in place and it still didn't work. It give me "the page can't be displayed" error and does not send an email. At this point, I don't care what the subject is, just that it works. lol. Thanks again!
Copy linkTweet thisAlerts:
@NogDogMar 13.2014 — Try changing this...
[code=php]
if(isset($_POST['name'])) {
[/code]
...to this...
[code=php]
if(isset($_POST['email_address'])) {
[/code]


(Notice how the $_POST fields are directly connected to name attributes of your form elements?)
Copy linkTweet thisAlerts:
@thefriz55authorMar 14.2014 — No go--still not work. ? Thanks again for the help!
Copy linkTweet thisAlerts:
@NogDogMar 14.2014 — I'd suggest you add the following to the top of your PHP script(s), run your tests, and show us the [i]exact[/i] error messages you get, along with the latest code you're working with:
[code=php]
<?php
ini_set('display_errors', true); // set to false for production
error_log(E_ALL);

// ...rest of script...
[/code]
Copy linkTweet thisAlerts:
@salmanshafiqMar 15.2014 — Here is the correct code......

HTML code...
<i>
</i>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Sales Contact Form&lt;/title&gt;
&lt;style type="text/css"&gt;
.container {
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: bolder;
width: 500px;
color: #000;


}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div class="container"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Contact Us&lt;/legend&gt;
&lt;form action="test.php" method="post" name="contact" id="contact_form"&gt;

&lt;table width="498" height="362" border="0" cellpadding="9"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label&gt;Name&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="name" type="text" class="name" size="40"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;label&gt;Email Address&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="email_address" type="text" class="required email" size="40"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; &lt;label&gt;Subject&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;select name="subject"&gt;
&lt;option value="Select one:" selected="selected"&gt;Select one:&lt;/option&gt;
&lt;option value="recipient_5"&gt;Cars&lt;/option&gt;
&lt;option value="recipient_2"&gt;Real Estate&lt;/option&gt;
&lt;option value="recipient_4"&gt;Equipment&lt;/option&gt;
&lt;option value="recipient_3"&gt;Events&lt;/option&gt;
&lt;option value="Recipient_6"&gt;Finance or Accounting &lt;/option&gt;
&lt;option value="Recipient_7"&gt;Website Related Matters&lt;/option&gt;
&lt;option value="recipient_1"&gt;Sales Manager&lt;/option&gt;
&lt;/select&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="left" valign="top"&gt;&lt;label&gt;Message&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;textarea name="message" cols="40" rows="15" &gt;&lt;/textarea&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;*&lt;/td&gt;
&lt;td&gt;&lt;input type="submit" value="Submit Form" style="font-family: 'Arial'; font-size: 12px; letter-spacing: 1px" /&gt;

<i> </i> &lt;input type="reset" value="Reset Form" style="font-family: 'Arial'; font-size: 12px; letter-spacing: 1px" /&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;/table&gt;




<i> </i>&lt;/form&gt;
&lt;/fieldset&gt;
&lt;/div&gt;


&lt;/body&gt;
&lt;/html&gt;



PHP code
<i>
</i>
&lt;?php <br/>
$state = 1; <br/>
if(isset($_POST['name'])) { <br/>
// multiple recipients <br/>
$to = ''; // note the comma <br/>
// subject <br/>
$subject = $_POST['subject']; <br/>
// message <br/>
$message = " <br/>
&lt;html&gt; <br/>
&lt;head&gt; <br/>
&lt;title&gt;".$_POST['subject']."&lt;/title&gt; <br/>
&lt;/head&gt; <br/>
&lt;body&gt; <br/>
&lt;p&gt; <br/>
Name: ".$_POST['name']." &lt;br /&gt;

<i> </i> Email: ".$_POST['email_address']." &lt;br /&gt;

<i> </i> Enquiry: ".$_POST['message']." &lt;br /&gt;
<i> </i> &lt;/p&gt;
<i> </i>&lt;/body&gt;
<i> </i>&lt;/html&gt;
<i> </i>";

<i> </i>echo $message;
<i> </i>// To send HTML mail, the Content-type header must be set
<i> </i>$headers = 'MIME-Version: 1.0' . "rn";
<i> </i>$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
<i> </i>// Additional headers
<i> </i>$headers .= 'To: recepient_1 &lt;'.$to.'&gt;' . "rn";
<i> </i>$headers .= 'From: recepient_1 &lt;'.$to.'&gt;' . "rn";


if($_REQUEST['subject']=='recipient_1' || $_REQUEST['subject']=='recipient_2' || $_REQUEST['subject']=='Recipient_6' || $_REQUEST['subject']=='Recipient_7' || $_REQUEST['subject']=='Recipient_8')
$to = '[email protected]';
if($_REQUEST['subject']=='recipient_3' || $_REQUEST['subject']=='recipient_4' || $_REQUEST['subject']=='recipient_5')
$to = '[email protected]';

$my_email = $recepient[$_REQUEST['subject']];

<i> </i>// Mail it
<i> </i>mail($to, $subject, $message, $headers);
<i> </i>$state = 2;

}
?&gt;

×

Success!

Help @thefriz55 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...