简体   繁体   中英

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

It is so that I have a checklist of names taken from the SQL database. When I select multi checkboxes and press submit then only on one email is sent and not the other ones I checked. I have verified it by typing echo "$_POST['check']";

What am i doing wrong here?

<?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');

Working PhpFiddle

Most of your code is working. The only problem I see is with how you're verifying the contents of an array.

Human readable PHP array

To make a PHP array human readable try using the print_r function like this:

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

I put this code in the PhpFiddle, and after you submit the form it will display a human readable version of the submitted array.

If you're still having problems with sending an email then your issue is probably with something besides your form posting more than one checkbox value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM