www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] php contact form

  1. #1
    Join Date
    Jun 2006
    Location
    Boston, MA
    Posts
    383

    resolved [RESOLVED] php contact form

    I have this simple php form which works, my problem is that it takes the user to a new page with, a single message once submitted. I'm pretty new to php and have pieced this together. Can anybody help me add an array of subject lines that corresponds with the email selected from the dropdown. I'm also looking to clear the contact box and add some 'your message has been sent' text above the box itself rather then change the page. Can anybody help me out?

    PHP Code:
    <?php

      
    // config
      
    $emailAddresses = array(
       
    'Sales'=>'sales@getrapidcharge.com',
       
    'Advertising'=>'advertising@getrapidcharge.com',
      );
      
    $emailSubject 'The subject line of the email';

      
    // 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 POST fields is missing.");
        }
        
    // 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 "Dear {$_POST['destemail']}\n\n{$_POST['name']} ({$_POST['srcemail']}) said:\n\n{$_POST['comment']}\n\nBest Regards\n\nSome random website";
        
    // Send the email and report the result
        
    if (mail($emailAddresses[$_POST['destemail']],$emailSubject,$emailBody,"From: {$_POST['srcemail']}")) {
          exit(
    "Email sent successfully");
        } else {
          exit(
    "Email sending failed");
        }
      }

      
    // Output the html form
    ?>
    HTML Code:
    <form action="" method="POST">
          <table style="border-width: 0px; border-style: none;">
            <tr>
              <td valign="top">Department</td>
              <td><select name="destemail">
    <?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 colspan="2"><input type="submit" /></td>
            </tr>
          </table>
        </form>

  2. #2
    Join Date
    May 2012
    Posts
    59
    There is nothing you don't already know PHP and HTML wise in order for you to create a solution to this problem.

    All you need are a few more variables, few more if's and elses, and a few print statements

    Don't give up and ask for help at the first sign of trouble. I'll help you out when you try some more.

    Kind regards,
    Scott

  3. #3
    Join Date
    Jun 2006
    Location
    Boston, MA
    Posts
    383
    i just logged in to mark this resolved. i guess i was just having a brain fart! one thing i can't get right is the email validation from the person submitting. got any suggestions? btw, i pieced that together from examples, i didn't write it.

  4. #4
    Join Date
    May 2012
    Posts
    59
    Yeah, use regular expressions to validate the email address is in the correct format.

    Google will give you a million examples.

    Kind regards,
    Scott

  5. #5
    Join Date
    May 2012
    Posts
    42
    I try to use built in functions for validation where I can. PHP has decent email validation with filter_var
    PHP Code:
    $is_valid filter_var($emailFILTER_VALIDATE_EMAIL); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles