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

时间:2017-08-11 04:59:55

标签: php

我是php的新手。我创建了一个包含各种字段和文件附件的表单。在google上进行了这么多搜索之后,我终于得到了php脚本来发送带附件的电子邮件。此脚本使用phpmailer发送表单数据和附件。一切都很好。我收到表单数据以及电子邮件中的附件。我的问题是附件是强制性的。如果用户没有附加文件,则会出错,并且不会提交表单。我希望将附件作为可选方式,即使用户没有上传文件,我应该在电子邮件中获取表单的其余部分,如果用户上传文件,我也应该获取附件。在我的html表单文件中,附件不是必填字段。有人可以帮助我吗?所有的提前感谢。我的php脚本如下:

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

这是我的html文件。

<!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>

也可以有人帮我这个。如何允许用户附加多个文件。什么是这个PHP?你能帮我解决这个问题吗?我今天学到了很多东西。我知道我必须在输入文件中添加多个,名称应为&#34; name []&#34;。现在你有我的html和php文件。 Saxena先生

0 个答案:

没有答案