2015-01-14 36 views
1

我有一个复选框,用户可以选择要下载哪个PDF。用户可以选择多个PDF ..提交..用户将得到一个电子邮件与下载链接。我设法向用户发送电子邮件,但无法提供基于他们从checkbox.any帮助选择什么样的下载链接,将不胜感激如果选中复选框,如何使用echo下载链接

<?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 (&nbsp;<input type='checkbox' name='checkall' onclick='checkedAll(subscribe);'><label>&nbsp&nbsp;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; 
?> 
+0

不知道你有什么问题 –

+0

即时无法回显基于复选框的下载链接他们打勾.. – ksonia90

回答

0

您的复选框有值(1,2,3),对不对?下一步是将这些值与您的pdf文件名相关联,以便稍后构建锚点。为了做到这一点,你可以设置一个数组,将复选框的值设置为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> 

我认为下载链接的第一部分是静态的,只有文件名更改。从评论


问:

可我知道为什么了2 $ pdf_filename?

$ link_template是一个字符串。它准备与sprintf()一起使用。 %表示占位符,s表示当由sprintf格式化时,参数被视为字符串并呈现。这就是为什么有字符串的占位符:两个%s

现在,当调用sprintf()时,第一个参数是模板字符串。 接下来的两个参数是包含值的变量,应该为%s占位符插入。

第一个占位符位于src标记内。这显然是文件名。 第二个占位符可能是文件名或复选框的值或修改后的文件名 - 这取决于您想要显示的锚点/链接内容。 例如,您可以截断文件扩展名并仅将one作为链接打印。

+0

我可以知道为什么得到2 $ pdf_filename吗? – ksonia90

+0

btw即时通讯在$ pdf_filenames – ksonia90

+0

意外的'=>'(T_DOUBLE_ARROW)错误Sry,我错过了在花括号前面添加'array'。它是'$ pdf_filenames = array('。我会追加我的答案,以回答你关于sprintf中2 $ pdf_filenames的问题。 –

相关问题