php contact form - add field and requirements

hey everyone. i pieced this together from examples and what not, i’m by no means a pro here. the only thing i changed was the result display. before it was exiting the page, so i changed it to display on the page itself.

i’d like to set the name, email, message fields as required and also add in a non-required phone number field but im at a loss as to how i can correctly put it together. any help or suggestions would be greatly appreciated.

[php]<?php

// config
$emailAddresses = array(
‘Sales’=>‘[email protected]’,
‘Advertising’=>‘[email protected]’,
);
$emailSubject = ‘Message From The Contact Page!’;

// If we are dealing with a form submission, send an email
if (isset($_POST[‘name’])) {
// Check all the fields are present
if (!isset($_POST[‘destemail’],$_POST[‘comment’],$_POST[‘srcemail’])) {
exit(“Sorry, at least one of the fields is empty.”);
}
// Check the email selected is valid
if (!isset($emailAddresses[$_POST[‘destemail’]])) {
exit(“Sorry, you have selected an invalid email option.”);
}
// Create the body of the email message
$emailBody = “{$_POST[‘destemail’]}\n\n{$_POST[‘name’]} ({$_POST[‘srcemail’]}) said:\n\n{$_POST[‘comment’]}”;
// Send the email and report the result
if (mail($emailAddresses[$_POST[‘destemail’]],$emailSubject,$emailBody,“From: {$_POST[‘srcemail’]}”)) {
$result = ‘’;
$results .= “

Email sent successfully!
”;
} else {
$results .= “
Email sending failed!
”;
}
}

// Output the html form
?>[/php]

So this looks like PHP code that processes form data and then tries to send an email. If you want to change the fields of the form, that is an HTML project. Maybe someone could help you with that, but it would be good to get the code.

hered my html form. i’m trying to make every form required, but i want to add an optional phone field. not even my damn email validator works though!

[code]




Department <?php foreach ($emailAddresses as $name => $email) { ?>
          <option value="<?php echo htmlspecialchars($name); ?>"><?php echo htmlspecialchars($name); ?></option>
<?php } ?>
        </select>  </td>
    </tr>
    <tr>
      <td valign="top">Name</td>
      <td><input type="text" name="name" /></td>
    </tr>
    <tr>
      <td valign="top">Email</td>
      <td><input type="text" name="srcemail" /></td>
    </tr>
    <tr>
      <td valign="top">Message:</td>
      <td><textarea cols="35" rows="8" name="comment"></textarea></td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td><input name="submit" type="submit" value="Send!" /></td>
    </tr>
  </table>
</form>[/code]

updated my php a bit.

[php]<?php

// config
$emailAddresses = array(
‘Sales’=>‘[email protected]’,
‘Advertising’=>‘[email protected]’,
);
$emailSubject = ‘Message From The Contact Page!’;

// If we are dealing with a form submission, send an email
if (isset($_POST[‘name’])) {
// Check all the fields are present
if (!isset($_POST[‘destemail’],$_POST[‘comment’],$_POST[‘srcemail’])) {
exit(“Sorry, at least one of the fields is empty.”);
}
// Check the email selected is valid
if (!isset($emailAddresses[$_POST[‘destemail’]])) {
exit(“Sorry, you have selected an invalid email option.”);
}
// Create the body of the email message
$emailBody = “{$_POST[‘destemail’]}\n\n{$_POST[‘name’]} ({$_POST[‘phone’]}) ({$_POST[‘srcemail’]}) said:\n\n{$_POST[‘comment’]}”;
// Send the email and report the result
if (mail($emailAddresses[$_POST[‘destemail’]],$emailSubject,$emailBody,“From: {$_POST[‘srcemail’]}”)) {
$result = ‘’;
$results .= “

Email sent successfully!
”;
} else {
$results .= “
Email sending failed!
”;
}
}

// Output the html form
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service