簡體   English   中英

即使選中更多復選框,PHP復選框列表也僅發送一個值

[英]PHP checkbox list only send one value even if more is checked

這樣我就有了一個從SQL數據庫中提取的名稱清單。 當我選擇多個復選框並按提交時,僅發送一封電子郵件,而不發送我檢查的其他電子郵件。 我已經通過輸入echo "$_POST['check']";

我在這里做錯了什么?

<?php include('includes/config.php'); 

if(isset($_POST['check']) == true)
{

    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);
    $from = 'noreply@email.com';
    $reply = 'reply@email.com';

    foreach($_POST['check'] as $key => $value)
    {
        // Set content-type for sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= "From: <".$from.">\r\n";
        $headers .= "Reply-To: ".$reply."";
        if(@mail($value,$subject,$message,$headers))
        {   
            echo '<div class="container-fluid" style="width:50%;">
                  <div class="alert alert-success fade in">
                  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
            echo '<strong>Success! </strong>'; 
            echo ' Mail has been Successfully sent to '.$value.'</br>';
            echo '</div></div>';
        } 
    }
}

?>

 <form method="post" action="">
    <?php

    // Retrieve Email from Database
    $getemail = mysql_query("SELECT * FROM Email_Users");

    if (!$getemail) die('MySQL Error: ' . mysql_error());

    echo '<table class="table table-bordered">';
    echo "<thead>
          <tr>
          <th><input type='checkbox' onchange='checkedbox(this)' name='chk'/></th>
          <th>Username</th>
          <th>Email</th> 
          </tr>
          </thead>";

    if (mysql_num_rows($getemail) == 0) {    
    echo "<tbody><tr><td colspan='3'>No Data Avaialble</td></tr></tbody>";    
    } 

    while ($row = mysql_fetch_assoc($getemail)) {     
        echo "<tbody><tr><td><input value='".$row['email']."' type='checkbox' name='check[]'/></td>";   
        echo "<td >".$row['username']."</td>";
        echo "<td >".$row['email']."</td></tr></tbody>";
    } 
    echo "</table>";
    ?>
    <p>Email Subject:<input type="text" name="subject" value=""  class="form-control"/></p>
    <p>Email Content:<textarea name="message" cols="40" rows="6"></textarea></p>
    <center><input type='submit' name='submit' value='Send Email Now' class="btn btn-primary btn-block"/>
    </center>

使用此jQuery代碼獲取復選框數組。

var checkboxes = $('input[name="chk"]:checked').prop('value');

工作PhpFiddle

您的大多數代碼都在工作。 我看到的唯一問題是如何驗證數組的內容。

可讀的PHP數組

要使PHP數組易於閱讀,請嘗試使用print_r函數,如下所示:

$array = $_POST['check'];
echo '<pre>'.print_r( $array, true ).'</pre>';

我將這段代碼放在PhpFiddle中,提交表單后,它將顯示人類可讀的提交數組版本。

如果您在發送電子郵件方面仍然遇到問題,那么您的問題可能與表單上發布多個復選框值之外的問題有關。

暫無
暫無

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

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