Question

I'm creating an application form which gets certain fields, such as there person's first name, last name, email, department, etc. and submits that information into email form. And everything works perfectly, but I want the department that the user selects to show in the subject line of the email. I made that portion into an option, instead of having the user type it in and use $_POST in my PHP email file. I'm sure it is minor but I am not sure on how it should be done. Are there any suggestions? Thanks in advance

----------------------------Here is the HTML Code-------------------------

    <body style="padding:3px; margin:0px;" bgcolor="#FFFFFF"> 
    <center>
    <table cellpadding="0" cellspacing="0" border="0" width="440"> 

        <tr><td style="height:10px"></td></tr> 
        <tr> 
          <td colspan="2" style="text-align:justify; line-height:15px;" class="body"> 

          <form name="frm" method="POST" action="php file" enctype="multipart/form-data"> 
          <center>
          <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
              .........
              .........
              .........
            <tr><td style="height:3px"></td></tr>
            <tr>
            <td width = "23%" class="body">Departments</td>
            <td width = "3%" class ="body">:</td>
            <td width="74%">
                <select>

     <option value="departments" name = "departments">Engineering</option>
     <option value="departments" name = "departments">Marketing</option>
     <option value="departments" name = "departments">PR/Social</option>
     <option value="departments" name = "departments">Media</option>
                </select>
            </td>
            </tr>

-------------------PHP--------------------------

       <?php

if(!empty($_FILES['resume_name']['name'])){

    $file_name=$_FILES['resume_name']['name'];
    $temp_name=$_FILES['resume_name']['tmp_name'];
    $file_type=$_FILES['resume_name']['type'];
    $file_size=$_FILES['resume_name']['size'];

    $base=basename($file_name);
    $extension= pathinfo($base, PATHINFO_EXTENSION);

    $allowed_ext=array("doc","docx","pdf","zip","jpeg","jpg","txt");

    if (in_array($extension,$allowed_ext)){
    $from=$_POST['email'];
    $fname=$_POST['first_name'];


    $lname=$_POST['last_name'];
        $to="travis.dacosta1@gmail.com";
        $subject= $departments;
        $credentials=$_POST['qualifications'];

        $message= 

        '           The person that contacted you is: '.$fname.' '.$lname.'
                E-mail: '.$from.' 
                Subject: '$subject'
                Qualifications: '.$credentials.'

        |---------END MESSAGE----------|';


        $file=$temp_name;
        $content = chunk_split(base64_encode(file_get_contents($file)));
        //$content = chunk_split(base64_encode(file_get_contents($content)));
        $uid=md5(uniqid(time()));


        $header =  "From:   ". $from. "\r\n";
        $header .= "Reply-To:   ". $replyto. "\r\n";
        $header .= "MIME-Version: 1.0\r\n";

        $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";


        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--".$uid."\r\n";

        $header .= "--".$uid."\r\n";
        $header .= "Content-type:   ".$file_type."; name=\"".$file_name."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: resume_name; file_name=\"".$file_name."\"";
        $header .= $content."\r\n\r\n";

        if(@mail($to, $subject, $message, $header)){
                echo "Success";
                }
                exit();
    ?>
Was it helpful?

Solution

You should set up value for your options, like this:

<option value="Engineering" name = "departments">Engineering</option>
<option value="Marketing" name = "departments">Marketing</option>
...

Than in your PHP put this:

$subject = $_POST['departments'];

This isnt best solution but it will work.

Better would be to define somewhere in PHP file (config) array with available departments $departments = array(1 => 'department1', 2 => 'department2'), in html template make foreach loop above array of departments set value to key in array and in email script use $deparments[intval($_POST['departments'])]

OTHER TIPS

I realized it was the way in which I was writing the header statement. Instead I changed it to:

    $header =  "From:   ". $from. "\r\n";
$header .= "Reply-To:   ". $replyto. "\r\n";
$subject .= "Applicant for the $subject1 Dept\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

Also I added the POST statement you suggested so it can take the input from the html file and utilize it in the php file. It now works like a charm. Thanks!!

with <select> dropdown the name should go in the select tag, not the options, then the $_POST['departments'] will be the 'value' of the selected option

<select name = "departments">

 <option value="engineering" >Engineering</option>
 <option value="marketing" >Marketing</option>
 <option value="social" >PR/Social</option>
 <option value="media" >Media</option>

I'm surprised this is working correctly for you, I'd have thought it always reverted to the final option (Media in this case) though I haven't tested that assumption....

check here for "allowed" attributes for select tags https://www.w3schools.com/tags/tag_select.asp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top