带有复选框、单选按钮和文件附件的PHP表单



我对这个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!

为了防止SPAM,我使用了一个技巧,其中有一个隐藏的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"] . "rn";
$message .= "E-mail: " . $_POST["email"] . "rn";
$headers  = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "Reply-To: " . $_POST["email"] . "rn";
$headers .= 'From: My Website <application@mywebsite.com>' . "rn";

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

所以所有的东西都被从先前的CCD_ 1变量中丢弃。

首先,从删除这条线路$message = $headers; 开始

并将其替换为该线路$message ="";

然后删除该行

$message= "
";

✓新代码体:


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

✓关于文件附件


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

它必须包括在内,看起来如下:

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

我建议你谷歌";如何用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 rn
$headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "rnContent-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";
?> 

相关内容

  • 没有找到相关文章

最新更新