0

我正在创建一个获取某些字段的申请表,例如某人的名字、姓氏、电子邮件、部门等,并将该信息提交到电子邮件表单中。一切正常,但我希望用户选择的部门显示在电子邮件的主题行中。我将该部分设置为一个选项,而不是让用户输入它并在我的 PHP 电子邮件文件中使用 $_POST。我确定这是次要的,但我不确定应该如何完成。有什么建议吗?提前致谢

----------------------------------------这是 HTML 代码----- --------

    <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();
    ?>
4

3 回答 3

1

您应该为您的选项设置值,如下所示:

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

比在你的 PHP 中放这个:

$subject = $_POST['departments'];

这不是最好的解决方案,但它会起作用。

更好的是在 PHP 文件(配置)数组中定义可用部门$departments = array(1 => 'department1', 2 => 'department2'),在 html 模板中在部门数组上方进行 foreach 循环设置值以键入数组和电子邮件脚本使用$deparments[intval($_POST['departments'])]

于 2013-12-22T19:26:22.740 回答
0

我意识到这是我编写标题语句的方式。相反,我将其更改为:

    $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";

我还添加了您建议的 POST 语句,以便它可以从 html 文件中获取输入并在 php 文件中使用它。它现在就像一个魅力。谢谢!!

于 2013-12-28T20:43:13.400 回答
0

使用<select>下拉列表,名称应该放在选择标签中,而不是选项中,然后 $_POST['departments'] 将是所选选项的“值”

<select name = "departments">

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

我很惊讶这对你来说是正确的,我认为它总是恢复到最终选项(在这种情况下是媒体)虽然我没有测试过这个假设......

在此处检查选择标签的“允许”属性 https://www.w3schools.com/tags/tag_select.asp

于 2019-03-01T14:38:03.627 回答