Leo
Leo

Reputation: 977

Multi selection input only submits the last selection

I have a simple form, inside it there is check-boxes and multi selection inputs.

The problem I am having is:

  1. The multi selection input only submits the last selection!
  2. The checkboxes only works if I set a value="" but shouldn't work without it?

Here is the form:

<fieldset>
    <p>Multi selection</p>
    <select name="dropdown2[]" class="select multi-select " data-placeholder="Choose an option" multiple="multiple">
        <optgroup label="Section">
            <option>Drop Down Option A</option>
            <option>Drop Down Option B</option>
        </optgroup>
        <optgroup label="Section">
            <option>Drop Down Option A</option>
            <option>Drop Down Option B</option>
        </optgroup>
    </select>
</fieldset>
<fieldset>
    <p>Optional checkboxs</p>
    <label class="checkbox">
        <input name="11" type="checkbox"><span><i></i></span>
        <p>CheckA</p>
    </label>
    <label class="checkbox">
        <input name="11" type="checkbox"><span><i></i></span>
        <p>CheckB</p>
    </label>
</fieldset>

Here is the PHP:

<?php
// Only start sessions if they haven't been already to prevent errors
if (empty($_SESSION)){session_start();}

// If 'data' var was received via POST from form-validation.js
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // There's not really a need for this line with modern browsers
    ob_start();

    // Open the div around the message
    $message = "<div style=\"styling stuff\">";

    // Loop through every single post value
    foreach ($_REQUEST as $key => $value) {
        // If it's not empty
        if (!empty($value)) {
            // Change the name attributes to look a bit more human-readable
            $thisKey = str_replace("-", " ", str_replace("_", "|", $key));

            // Populate the message var
            $message .= "<strong>" . $thisKey . ":</strong> " . $value . "<br />";
        }
    }
    // Close the div around the message
    $message .= '</div>';

    // Mail variables
    $to = '[email protected]';
    $subject = 'New Message';
    $headers = "From: [email protected]\r\n";
    $headers .= "Reply-To: [email protected]\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    // Attempt to send
    $sendMail = @mail($to, $subject, $message, $headers);

    // If it fails...
    if (!$sendMail) {
        // Terminate processing with error
        die("There was a problem sending the email");
    } else {
        // Terminate processing with success msg
        die("Email was sent!");
    }

    // As above, no real need for this line with modern browsers
    ob_flush();

    // Terminate
    die();
}
?>

Live preview: http://loaistudio.com/contact

Upvotes: 1

Views: 263

Answers (2)

Basit
Basit

Reputation: 1840

If you want to send multiple values via checkboxes, then they should be used in this way

<input name="11[]" type="checkbox"> 
<input name="11[]" type="checkbox">

instead of

<input name="11" type="checkbox"> 

Upvotes: 1

Dhiraj Wakchaure
Dhiraj Wakchaure

Reputation: 2706

if you need to submit multi check box then use like this

<input name="11[]" type="checkbox" value="someval"> 
<input name="11[]" type="checkbox" value="someval"  > 

this will post checkbox elements as array

to retrieve

$chk = $_POST['11']; // this will be array  

iterate through this array you will get all checked elements of name 11

still you need to provide some values to check box to work this

Upvotes: 0

Related Questions