簡體   English   中英

帶復選框,單選按鈕和文件附件的PHP表單

[英]PHP form with checkboxes, radio buttons and file attachment

我在使用此PHP表單時遇到了噩夢,用戶可以在其中填寫,附加doc / pdf並提交。 在嘗試使用我以前使用的代碼進行整理之后,我將其刪除,並認為我應該重新開始,希望天才能為您提供幫助!

這是contact.php的HTML:

<form action="" method="post" name="contact" id="contact">

<p>Job Title:*<br />
<input name="position" type="text" /></p>

<p>Nationality:*<br />
<select name="nationality">
  <option value="">-- select one --</option>
  <option value="Afghan">Afghan</option>
  <option value="Albanian">Albanian</option>
  <option value="Algerian">Algerian</option>
  <option value="Zambian">Zambian</option>
  <option value="Zimbabwean">Zimbabwean</option>
</select>
</p>

<p>Which country are you currently living in?*<br />
<select name="country">
<option value="">-- select one --</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Africa">Africa</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option>
</select>
</p>

<label class="radio" for="checkRight">Yes/No question?</label><br />
<input class="radio" type="radio" name="right" value="Yes" /> Yes
<input class="radio" type="radio" name="right" value="No" /> No
<input class="radio" type="radio" name="right" value="N/A" /> Not applicable

<p>Yes/No question?<br />
<select name="continue">
<option value="">-- select one --</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</p>

<p>Select your resorts:<br />
Resort 1<input name="res1" type="checkbox" value="Resort 1" />
Resort 2<input name="res2" type="checkbox" value="Resort 2" />
Resort 3<input name="res3" type="checkbox" value="Resort 3" />
Resort 4<input name="res4" type="checkbox" value="Resort 4" />
Resort 5<input name="res5" type="checkbox" value="Resort 5" />
Resort 6<input name="res6" type="checkbox" value="Resort 6" />    
</p>

<p>Don't send form unless this is checked:* <input type="checkbox" name="parttime" value="Yes" /></p>

<p>Date of arrival: <input name="arrive" id="datepick" /><br />
Date of departure: <input name="depart" id="datepick2" /></p>

<script type="text/javascript" src="assets/scripts/datepickr/datepickr.js"></script>
<link href="assets/scripts/datepickr/datepickr.css" rel="stylesheet">

<script type="text/javascript">
new datepickr('datepick');
new datepickr('datepick2', {
});
</script>


<p>Name:*<br />
<input name="name" type="text" /></p>

<p>E-mail:*<br />
<input name="email" type="text" /></p>

<p>Telephone:*<br />
<input name="telephone" type="text" class="ctextField" /></p>

<p>Upload CV (Word of PDF formats only):<br />
<input type="file" name="cv" class="textfield"></p>

<p><input name="submit" value="Submit Enquiry" class="submitButton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p>

</form>

順便說一下,日期框可以正常工作了,請原諒其中的Javascript!

為了防止垃圾郵件,我使用了一個技巧,其中有一個隱藏的URL字段,必須將其保留為空白才能提交要在PHP中看到的表單。

以下是我使用的PHP,它位於contact.php標頭上方。

<?php

if (array_key_exists('submit', $_POST)) {
$position = $_POST['position'];
$arrive = $_POST['arrive'];
$nationality = $_POST['nationality'];
$parttime = $_POST['parttime'];
$depart = $_POST['depart'];
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];

$to = "me@mywebsite.com";
$subject = "Recruitment Application";

$message = $headers;
$message .= "Name: " . $_POST["name"] . "\r\n";
$message .= "E-mail: " . $_POST["email"] . "\r\n";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= 'From: My Website <application@mywebsite.com>' . "\r\n";



$message= "
";

$url = stripslashes($_POST["url"]);
if (!empty($url)) {
header( 'Location: http://www.go-away-spam-robots.com' );
exit();
}

if (!isset($warning)) {
mail($to, $subject, $message, $headers);
header( 'Location: http://www.mywebsite.co.uk/sent.php' );
}

}

?>

我希望所有字段都是必填字段,因此,如果某個字段保留為空(隱藏URL字段除外),則該字段旁邊會顯示一條警告消息。

另外,我希望將文件上傳字段附加到發送給我的電子郵件中,並以表格格式將結果發送給我。

誰能幫我使我的表格生效?

謝謝,希望能收到您的來信!

SM

✓經過測試


有一些錯誤,並在下面進行了測試。 文件附件部分,您將需要進行調查。 我不寫代碼,我修復了可以使用的代碼。

發生的情況是您正在用此行覆蓋消息。

$message= "
";

因此所有內容都會從之前的$message變量中丟棄。

首先,首先刪除此行$message = $headers;

並將其替換為此行$message ="";

然后刪除此行

$message= "
";

✓新的代碼體:


$message ="";
$message .= "Name: " . $_POST["name"] . "\r\n";
$message .= "E-mail: " . $_POST["email"] . "\r\n";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= 'From: My Website <application@mywebsite.com>' . "\r\n";

✓關於文件附件


為了附加文件,您的表單不包含enctype="multipart/form-data"

它必須包括在內,並且看起來如下所示:

<form action="" method="post" name="contact" id="contact" enctype="multipart/form-data">

我建議您Google“如何在PHP中將文件附加到電子郵件”或“在PHP中將文件附加到電子郵件”。

您可以在StackOverflow上查看一個示例和一個可接受的答案以了解如何實現此目的。


✓示例文件附件PHP處理程序


這是有關如何附加文件的示例PHP處理程序。

來源: http//webcheatsheet.com/PHP/send_email_text_html_attachment.php

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM