我有一个复选框,用户可以在其中选择他们想要下载的 PDF。用户可以选择多个 PDF..一旦提交..用户将收到一封包含下载链接的电子邮件。我设法给用户发了电子邮件,但无法根据他们从复选框中选择的内容提供下载链接。非常感谢任何帮助
<?php
session_start();
require_once('/config/database.php') ;
error_reporting(0);
//if(isset($_POST['submit'])){
if(isset($_POST) && ($_POST['submit'] == 1)){
$error = '';
$name = $_POST['name'];
$email = $_POST['email'];
$Country = $_POST['Country'];
$checkBox = implode('<br>', $_POST['download']);
$registered_at = date('Y-m-d H:i:s');
//foreach($_POST['download'] as $value) {
//$check_msg .= "Checked: $value\n";
}
if ($_POST['secCode'] != $_SESSION['securityCode']) {
// unset($_SESSION['securityCode']);
$error .= "<script>alert('Sorry the security code is invalid. Please try it again')</script>";
}
if ($error == ''){
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }
$sql = "INSERT INTO users (name, email, country,download, registered_at)
VALUES ('$name','$email', '$Country', '" . $checkBox . "', '$registered_at' )";
if (mysql_query($sql)){
//email function start
//html checkbox
<div class="control-group">
<div class="controls">
<p><h5>Select device to download ( <input type='checkbox' name='checkall' onclick='checkedAll(subscribe);'><label>  Select all</label>) </h5></p>
<!--DIAGNOSTIC DEVICES-->
<input id="1" class="placeholder span4_1" type="checkbox" name="download[]" value="1"><label>1</label><br>
<input id="2" class="placeholder span4_1" type="checkbox" name="download[]" value="2"><label>2</label><br>
<input id="3" class="placeholder span4_1" type="checkbox" name="download[]" value="3"><label>3</label> <br>
<input id="4" class="placeholder span4_1" type="checkbox" name="download[]" value="4"><label>4</label><br>
<input id="5" class="placeholder span4_1" type="checkbox" name="download[]" value="5"><label>5</label><br>
<input id="6" class="placeholder span4_1" type="checkbox" name="download[]" value="6"><label>6</label> <br>
<!--THERAPEUTIC DEVICES-->
<input id="7" type="checkbox" name="download[]" value="7"><label>7</label><br>
<input id="8" type="checkbox" name="download[]" value="8"><label>8</label><br>
<span class="help-inline"></span>
</div>
</div>
下面是他们将收到的带有下载链接的电子邮件。从这里我可以回应他们想要下载的设备,但无法为他们提供链接。
<html>
<head></head>
<body>
<b><font size="4"> Download Link</font></b><BR><BR>
<p>Dear <?php echo $name ?>,</p>
<p>Thank you for signing up at <a href="http://localhost/test/" target="_blank"> test.com</a>.<br>
<p>Following is the link to download the device pdf that you have selected.</p><br>
<?php
echo $checkBox;
?>
最佳答案
您的复选框的值为 (1,2,3),对吗?下一步是将这些值与您的 pdf 文件名相关联,以便稍后构建 anchor 。为此,您需要为复选框值与 pdf 文件关系设置一个数组。例如,如果选中复选框 1
,则 pdf 名称为 one.pdf
。
// checkbox value to pdf filenames array
$pdf_filenames = array(
'1' => 'one.pdf',
'2' => 'two.pdf'
);
// $selected is the value of the selected checkbox
// fetch it from $_POST['download']
$pdf_filename = $pdf_filenames[$selected];
// build anchor template for the email
$link_template = '<a src="http://the.website/downloads/%s">Download %s</a>';
// insert the values via sprintf (maybe the second one is $selected, dunno)
$link = sprintf($link_template, $pdf_filename, $pdf_filename);
// for usage in the email template
echo $link;
// result: <a src="http://the.website/downloads/one.pdf">Download one.pdf</a>
我假设下载链接的第一部分是静态的,只有文件名发生了变化。
来自评论的问题:
may i know why got 2 $pdf_filename?
$link_template 是一个字符串。它准备与 sprintf() 一起使用。
%
标记一个占位符,s
表示当由 sprintf 格式化时,参数被视为并显示为字符串。这就是为什么有字符串占位符的原因:两个 %s
。
现在,当调用 sprintf() 时,第一个参数是模板字符串。 接下来的两个参数是包含值的变量,应该为 %s 占位符插入。
第一个占位符在 src 标签内。明明是文件名。
第二个占位符可能是文件名或复选框的值或修改后的文件名 - 这取决于您要显示为 anchor /链接内容的内容。
例如,您可以截断文件扩展名并只打印 one
作为链接。
关于php - 如果选中复选框,如何使用 echo 下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934516/