How do I submit an array from a form

Hello,

<form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">    
<table>
    	<tr>
    		<td>&nbsp;</td>
    		<td><input type="checkbox" name="user_group[]" value="1"></td>
    	</tr>
    	<tr>
    		<td>&nbsp;</td>
    		<td><input type="checkbox" name="user_group[]" value="2"></td>
    	</tr>
    	<tr>
    		<td><input type="submit" class="button" name="toplu_email_mesaji" value=" Gönder " /></td>
    		<td><input type="reset" class="button" value="Sıfırla" /></td>
     	</tr>   
    </table>
</form>
    $usergroup = implode(",", $_POST['user_group']);

    $groups = $mysqlibag->query("SELECT * FROM users WHERE user_group IN (".$usergroup.")");

    while ($row = $groups->fetch_assoc()) {
    // Here's the PHPmailer code to send bulk messages to members
    }

This code does not work
Where’s the error?
Can you help me

First of all (to be sure) place your form handling above your HTML and check :

  • If a form has been submitted
  • If the user has submitted valide values
<?php

// Is the form submitted?
if($_SERVER['REQUEST_METHOD'] == 'POST' )
{
    // do some form validation

    // if the validation was succesfull
    if($validation === true) 
    {
        // save the data to the database, send an email or do what you have to do

        // redirect client and stop this script
        header('Location: confirmation.php');
        exit;
    }
}
?>
<!-- Here comes the HTML -->

This code does not work
Where’s the error?

As you could read from the manual mysqli::query gives a FALSE on failures. So you should always test if mysqli::query gives a FALSE or not. If so you can use mysqli::$error to display the error message.

$groups = $mysqlibag->query("SELECT * FROM users WHERE user_group IN (".$usergroup.")");
if($groups === false) {
    echo $mysqlibag->error;
}

If you doubt about the content of $_POST you can easly dump it in a human readable format with

echo '<pre>' . print_r($_POST, true) . '</pre>';

Update your question if you have more information.

Better remove the complete action attribute for security reasons

Is only required if you use a file upload button.

Without a file upload you would then get:

<form method="post">
1 Like


2) <input type=“checkbox”
2) <input type=“text”

  1. textarea (contains html)

Note: This page is only visible to administrators
Admin to send a bulk message to some members

I solved the problem
Mail-$> Send (); not out of loop.
Will be in loop

I have implemented the above suggestions
Thank you for your help

    while ($row = $groups->fetch_assoc()) {
    // Here's the PHPmailer code to send bulk messages to members
$mail->Send(); // true
    }
$mail->Send(); // false
Sponsor our Newsletter | Privacy Policy | Terms of Service