doutuo4285 2017-08-11 04:59
浏览 33

使文件附件可选而不是强制

I am new to php. I have created a form with various fields and a file attachment. After doing so many searches on google, I finally got the php script to send email with attachments. This script uses phpmailer to send form data as well as attachment. Everything is working fine. I am getting the form data as well as attachments in email. My problem is that the attachment is compulsory. If the user does not attach file, it gives an error and the form is not submitted. I want to have the attachment as optional means even if the user does not upload the files, I should get the rest of the form in email and if the user uploads the files, I should get the attachments too. In my html form file attachment is not a required field. Can somebody help me in this? All the thanks in advance. My php script is as follows:

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$to = "saxena@designmaniac.in";
$mail->AddAddress($to);
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name_f'];
$mail->Subject = "Admission Form";

$body = "<table>
<tr>
<th colspan='2'>Admission Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Course :</td>
<td>".$_POST['course']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Payment Mode :</td>
<td>".$_POST['p_mode']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Payment options :</td>
<td>".$_POST['p_option']."</td>
</tr>

<tr>
<td style='font-weight:bold'>First Name :</td>
<td>".$_POST['name_f']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Middle Name : </td>
<td>".$_POST['name_m']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Last Name : </td>
<td>".$_POST['name_l']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-mail : </td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Date of Birth : </td>
<td>".$_POST['date']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Gender : </td>
<td>".$_POST['gender']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Nationality : </td>
<td>".$_POST['nationality']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Address : </td>
<td>".$_POST['address_line1']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City : </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Zip Code : </td>
<td>".$_POST['zip']."</td>
</tr>

<tr>
<td style='font-weight:bold'>State : </td>
<td>".$_POST['state']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Country : </td>
<td>".$_POST['country']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile Number : </td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother First Name : </td>
<td>".$_POST['fname_f']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother Middle Name : </td>
<td>".$_POST['fname_m']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother Last Name : </td>
<td>".$_POST['fname_l']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother Mobile No : </td>
<td>".$_POST['mobile_number1']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Qualification : </td>
<td>".$_POST['qualification']."</td>
</tr>

<tr>
<td style='font-weight:bold'>University : </td>
<td>".$_POST['university']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Year : </td>
<td>".$_POST['year']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Qualification : </td>
<td>".$_POST['qualification_2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>University : </td>
<td>".$_POST['university_2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Year : </td>
<td>".$_POST['year_2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Qualification : </td>
<td>".$_POST['qualification_3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>University : </td>
<td>".$_POST['university_3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Year : </td>
<td>".$_POST['year_3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Discipline Information (School) : </td>
<td>".$_POST['discipline']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Discipline Information (Violation) : </td>
<td>".$_POST['violation']."</td>
</tr>

<tr>
<td style='font-weight:bold'>12th Mark List : </td>
<td>".$_POST['attachment1']."</td>
</tr>

<tr>
<td style='font-weight:bold'>12th Passing Certificate : </td>
<td>".$_POST['attachment2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>10th Mark List : </td>
<td>".$_POST['attachment3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>10th Passing Certificate : </td>
<td>".$_POST['attachment4']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Address Proof : </td>
<td>".$_POST['attachment8']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Photographs : </td>
<td>".$_POST['attachment9']."</td>
</tr>

<table>";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = "saxena@designmaniac.in"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("saxena@designmaniac.in");
$mail->AltBody = "To view the message, please use an HTML compatible email 
viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap

$mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
$_FILES['fileToUpload']['name']);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>

This is my html file.

<!DOCTYPE HTML>
<html>
<head>
<title>Student Admission Form</title>
<link href="css/admn_form.css" rel="stylesheet">
<script>
function showMe(e) {
    var strdisplay = e.options[e.selectedIndex].value;
    var e = document.getElementById("idShowMe");
    if(strdisplay == "Online Payment") {
        e.style.display = "block";
    } else {
        e.style.display = "none";
    }
}

</script>
</head>
<body>

    <form name="admissionForm" id="admissionForm"
class="form-inline" action="php/email_handler_attachments.php" 
method="post" enctype='multipart/form-data'>
<fieldset>
<div class="container">

<center><label><h1 class="well" style="color:#fff44f;">Admission Application</h1></label>

</center><div class="col-lg-12 well">

<div class="row">
<form>
<div class="col-sm-12">

<div class="row">
<div class="col-sm-6 form-group">
<table>
<label>Course Details</label>
<p>
<tr>
<th>Course Applied For*</th>
<th>
 <select name="course" id="course" required>
  <option value=""></option>
  <option value="B.Sc. in Media Graphics & Animation">B.Sc. in Media Graphics & Animation</option>
  <option value="Diploma in Filmology">Diploma in Filmology</option>
  <option value="Post Graduate Diploma in Graphic Multimedia Animation">Post Graduate Diploma in Graphic Multimedia Animation</option>
  <option value="Diploma in Visual Effects (VFX)">Diploma in Visual Effects (VFX)</option>
  <option value="Diploma in Int-Ext Apps">Diploma in Int-Ext Apps</option>
  <option value="Advance Diploma in Software Development">Advance Diploma in Software Development</option>
  <option value="Diploma in Web Design & Development">Diploma in Web Design & Development</option>
  <option value="Diploma in Multimedia">Diploma in Multimedia</option>
  <option value="Diploma in Fine Arts">Diploma in Fine Arts</option>
</th>
</select>
</tr>
</p>
<p>
<tr>
<th>Payment Mode*</th>
<th>
 <select name="p_mode" id="p_mode" class="form-control" required>
  <option value=""></option>
  <option value="Lump Sum">Lump Sum</option>
  <option value="Yearly">Yearly</option>
  <option value="Semester Wise">Semester Wise</option>
  <option value="Monthly Payment">Monthly Payment</option>
  </th>
</select>
</tr>
</p>
<p>
<tr>
<th>Payment Options*</th>
<th>
 <select onchange="showMe(this);" name="p_option" id="p_option"  class="form-control">
  <option value=""></option>
  <option value="Online Payment">Online Payment</option>
  <option value="Cheque/Demand Draft">Cheque/Demand Draft</option>
  <option value="Cash Payment">Cash Payment</option>
  </th>
</select>
</p>
</table>
<table>
<tr>
<div id="idShowMe" style="display: none"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <a href="https://easebuzz.in/pay/Designmaniac" target="_blank" class="button" name="button_1" id="button_1">Proceed To Payment</a>
</div>
</tr>

</table>
</div>
<table>
_________________________________________________________________________________________________________________________________________________________________________________________________
</table>
<div class="row">
<div class="col-sm-6 form-group">
<p>
<label>Personal Information</label>
</p>
<table>
<tr>
<th>First Name*</th>
<th>Middle Name*</th>
<th>Last Name*</th>
</tr>
<tr>
<td><input type="text" name="name_f" type="text" pattern ="[a-z,A-Z, ]*" placeholder="" class="form-control" required></td>
<td><input type="text" name="name_m" type="text" pattern ="[a-z,A-Z, ]*" placeholder="" class="form-control" required></td>
<td><input type="text" name="name_l" type="text" pattern ="[a-z,A-Z, ]*" placeholder="" class="form-control" required></td>
</tr>
<p>(Please enter your name as it appears in your S.S.C/H.S.C. or other official documents.)
</p>
<tr>
<th>E-mail id*</th>
<th></th>
<th>Date of Birth*</th>
</tr>
<tr>
<td><input type="text" name="email" id="email" type="email" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$"  placeholder="" class="form-control" required></td>
<td></td>
<td><input type="date" value""... name="date" id="date"  placeholder=""style="width:395px;height:37px;border-radius: 4px"; required class="form-control"></td>
</tr>
<tr>
<th>Gender*</th>
<th></th>
<th>Marital Status*</th>
</tr>
<tr>
<td><select name="gender" id="gender" required>
  <option value=""></option>
  <option value="Male">Male</option>
  <option value="Female">Female</option></td>
</select>
<td></td>
<td><select name="gender_marital" id="gender" required>
  <option value=""></option>
  <option value="Single">Single</option>
  <option value="Married">Married</option></td>
</select>
</tr>
<tr>
<th>Nationality*</th>
<th></th>
<th>If Foreign National Give Your Passport and Visa Details</th>
</tr>
<tr>
<td><select name="nationality" id="nationality" required>
  <option value=""></option>
  <option value="Indian">Indian</option>
  <option value="Foreign National">Foreign National</option></td>
</select>
<td>
<td><input type="text" name="visa" id="visa" type="text" placeholder="" class="form-control"></td>
</tr>
</table>
</div>
</div>
<table>
_________________________________________________________________________________________________________________________________________________________________________________________________
</table>
<div class="row">
<div class="col-sm-6 form-group">
<p>
<label>Permanent Address</label>
</p>
<table>
<tr>
<th>Address Line 1*</th>
<th></th>
<th>Address Line 2</th>
</tr>
<tr>
<td><textarea placeholder="" name="address_line1" id="address_line1" type="text" rows="3" style="height:33px";  class="form-control" required></textarea>

</td>
<td></td>
<td><textarea placeholder="" name="address_line2" id="address_line2" type="text" rows="3" style="height:33px";  class="form-control"></textarea>

</tr>
<tr>
<th>City / Town*</th>
<th></th>
<th>Zip / Postal Code*</th>
</tr>
<tr>
<td><input type="text" id="address_line2" name="city" type="text" required placeholder="" class="form-control"></td>
<td></td>
<td><input type="text" id="address_line2" name="zip" type="number" pattern="^([0-9]{1,12})$" title='Zip Code (Format: 123456)' required placeholder="" class="form-control"></td>
</tr>
<tr>
<th>State / Province*</th>
<th></th>
<th>Country*</th>
</tr>
<tr>
<td><input type="text" id="address_line2" name="state" type="text" placeholder="" class="form-control" required></td>
<td></td>
<td><input type="text" id="address_line2" name="country" type="text" placeholder="" class="form-control" required></td>
</tr>
<tr>
<th>Phone Number</th>
<th></th>
<th>Mobile Number*</th>
</tr>
<tr>
<td><input type="text" name="phone_number" id="phone" type="number"  placeholder="Begin with Area or Country Code.." class="form-control"></td>
<td></td>
<td><input type="text" name="mobile_number" id="mobile" type="number" pattern='[\+]\d{2}\d{2}\d{8}' title='Phone Number (Format: +919999999999)'
placeholder="" class="form-control" required></td>
</tr>
</table
</div>
</div>
<table>
_________________________________________________________________________________________________________________________________________________________________________________________________
</table>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<p>
<label>Parent Information</label>
</p>
<p>
<table>
<b>Father's / Mother's Details</b>
</p>
<tr>
<th>First Name*</th>
<th>Middle Name*</th>
<th>Last Name*</th>
</tr>
<tr>
<td><input type="text" id="p_name" name="fname_f" type="text" pattern ="[a-z,A-Z, ]*" placeholder="" class="form-control" required></td>
<td><input type="text" id="p_name" name="fname_m" type="text" pattern ="[a-z,A-Z, ]*" placeholder="" class="form-control" required></td>
<td><input type="text" id="p_name" name="fname_l" type="text" pattern ="[a-z,A-Z, ]*" placeholder="" class="form-control" required></td>
</tr>
<tr>
<th>E-mail id</th>
<th></th>
<th>Mobile Number*</th>
</tr>
<tr>
<td><input type="text" type="email" name="email1" id="email1" placeholder="" class="form-control"></td>
<td></td>
<td><input type="text" name="mobile_number1" id="mobile" type="number" pattern='[\+]\d{2}\d{2}\d{8}' title='Phone Number (Format: +919999999999)'
placeholder="" class="form-control"></td>
</tr>
<tr>
<th>Profession</th>
<th></th>
<th>Position</th>
</tr>
<tr>
<td><input type="text" name="profession" id="prof" type="text" placeholder="" class="form-control"></td>
<td></td>
<td><input type="text" name="position" id="psn" type="text" placeholder="" class="form-control"></td>
</tr>
</table>

<table>
_________________________________________________________________________________________________________________________________________________________________________________________________
</table>
<div class="row">
<div class="col-sm-6 form-group">
<p>
<label>Your Academic Details</label>
</p>
<table>
<tr>
<th>Qualification</th>
<th>University / Board</th>
<th>Year</th>
</tr>
<tr>
<td><input type="text" name="qualification" id="board" type="text" placeholder="" class="form-control"></td>
<td><input type="text" name="university" id="board" type="text" placeholder="" class="form-control"></td>
<td><input type="text" name="year" id="board" type="number" placeholder="" class="form-control"></td>
</tr>
<tr>
<th>Qualification</th>
<th>University / Board</th>
<th>Year</th>
</tr>
<tr>
<td><input type="text" name="qualification_2" id="board" type="text" placeholder="" class="form-control"></td>
<td><input type="text" name="university_2" id="board" type="text" placeholder="" class="form-control"></td>
<td><input type="text" name="year_2" id="board" type="number" placeholder="" class="form-control"></td>
</tr>
<tr>
<th>Qualification</th>
<th>University / Board</th>
<th>Year</th>
</tr>
<tr>
<td><input type="text" name="qualification_3" id="board" type="text" placeholder="" class="form-control"></td>
<td><input type="text" name="university_3" id="board" type="text" placeholder="" class="form-control"></td>
<td><input type="text" name="year_3" id="board" type="number" placeholder="" class="form-control"></td>
</tr>
</table>
<table>
_________________________________________________________________________________________________________________________________________________________________________________________________
</table>
<div class="row">
<div class="col-sm-4 form-group">
<p>
<label>Discipline Information*</label>
</p>
<table>
<tr>
<td>Have you ever been placed on probation, suspended, removed,
dismissed or expelled from<br> any school or academic program since 10th grade?</td>
<td><input type="radio" name="discipline" value="yes" class="form-control required">Yes&nbsp;&nbsp;
  <input type="radio" name="discipline" value="no" class="form-control" required>No</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>Have you ever been charged with a violation of the law which resulted in,<br> or if still 
pending could result in, probation, a jail sentence,<br> or the revocation or suspension
 of your driver’s license<br> (including traffic violations which resulted in a fine of Rs.200 or more?)</td>

<td><input type="radio" name="violation" value="yes" class="form-control" required>Yes&nbsp;&nbsp;
  <input type="radio" name="violation" value="no" class="form-control" required>No</td>
</tr>
</table>
<table>
_________________________________________________________________________________________________________________________________________________________________________________________________
</table>
<div class="row">
<div class="col-sm-6 form-group">
<p>
<label>Attachments</label>
</p>
<table>
<tr>
<td>12th Mark List</td>
<td><input type="checkbox" name="attachment1" value="12th Mark list"></td>                
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td>12th Passing Certificate(To be submitted after receiving from school)</td>
<td><input type="checkbox" name="attachment2" value="12th Mark list"></td>                
</tr>
<tr>
<td>10th Mark List</td>
<td><input type="checkbox" name="attachment3" value="10th Mark list"></td>                
</tr>
<tr>
<td>10th Passing Certificate</td>
<td><input type="checkbox" name="attachment4" value="10th Mark list"></td>                
</tr>
<tr>
<td>Migration Certificate</td>
<td><input type="checkbox" name="attachment5" value="migration certificate"></td>                
</tr>
<tr>
<td>Transfer Certificate</td>
<td><input type="checkbox" name="attachment6" value="transfer certificate"></td>                
</tr>
<tr>
<td>Gap Certificate</td>
<td><input type="checkbox" name="attachment7" value="gap certificate"></td>                
</tr><tr>
<td>Address Proof</td>
<td><input type="checkbox" name="attachment8" value="address proof"></td>                
</tr>
<tr>
<td>Photographs</td>
<td><input type="checkbox" name="attachment9" value="photographs"></td>                
</tr>    
</table>
<table>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<p>
<label>Please Select the Files to Upload</label>
<td><input type="file" name="fileToUpload" id="fileToUpload" size="40"></td>
</p>
</tr>
</table>
<center><p>
<input type="submit" name="Submit" value="submit">
</p>
</center>
</div>
</div>
</div>
</form>
</body>
</html>

Also can somebody help me on this. How can I allow the user to attach multiple files. What will be the php for this? Could you please help me on this also? I have learnt a lot today. I know that I have to add multiple in input file and the name should be "name[]". Now you have both my html as well as php file. Saxena

  • 写回答

2条回答 默认 最新

  • dongqian5569 2017-08-11 05:01
    关注

    just add condition like this

    if(isset($_FILES['fileToUpload']['tmp_name']) && $_FILES['fileToUpload']['tmp_name'] != "") {
      $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
      $_FILES['fileToUpload']['name']);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!