多选输入仅提交最后一个选择

时间:2014-08-12 09:39:50

标签: php forms

我有一个简单的表格,里面有复选框和多选输入。

我遇到的问题是:

  1. 多选输入仅提交最后一个选择!
  2. 只有设置了值=""复选框才有效。但没有它就不应该工作?
  3. 以下是表格:

    <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>
    

    这是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@email.com';
        $subject = 'New Message';
        $headers = "From: email@email.com\r\n";
        $headers .= "Reply-To: email@email.com\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();
    }
    ?>
    

    实时预览:http://loaistudio.com/contact

2 个答案:

答案 0 :(得分:1)

如果您想通过复选框发送多个值,则应以这种方式使用它们

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

而不是

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

答案 1 :(得分:0)

如果您需要提交多重复选框 像这样使用

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

这会将复选框元素作为数组

发布

检索

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

遍历此数组,您将获得名称为11的所有已检查元素

仍然需要提供一些值来复选框才能使用