html表单元素并使用php获取数据

时间:2016-06-09 15:28:01

标签: php html email

我已经搜索并找到了一些很好的潜在解决方案,但似乎无法使这项工作成功。我还是个初学者。我正在使用带有下拉列表的表单元素,并尝试获取用户输入,然后通过电子邮件发送信息。我将在这里发布html和php:

            <form method = "post">
            <fieldset class="form-group">
                <div class="dropdown">
                  <button class="btn btn-primary-outline dropdown-toggle" type="button" data-toggle="dropdown" name="size">Choose Size&nbsp;
                  <span class="caret"></span></button>
                  <ul class="dropdown-menu">
                    <li>14 x 20</li>
                    <li>16 x 26</li>
                    <li>18 x 30</li>
                    <li>20 x 32</li>
                    <li>22 x 38</li>
                    <li>28 x 44</li>
                  </ul>
                </div>
            </fieldset>


            <div class="dropdown">
              <button class="btn btn-primary-outline dropdown-toggle" type="button" data-toggle="dropdown" name="color">Choose Color
              <span class="caret"></span></button>

                <ul class="dropdown-menu">
                    <li>Blue</li>
                    <li>Lt Blue</li>
                    <li>Purple</li>
                    <li>Lavendar</li>
                    <li>Green</li>
                    <li>Lt Green</li>
                    <li>Black</li>
                    <li>Pink</li>

                </ul>
            </div>

和PHP:

<?php

$error = ""; $successMessage = "";

if (isset($_POST['size'])) {
    $size = $_POST['size'];
    $size_string = "";
    for($i=0;$i<count($size);$i++) {

        if($i!=0) {

            $size_string = $size_string . ", ";

        }

        $size_string = $size_string . $service[$i];

    }

} else {

    $error .= "Please select a size from the dropdown menu.<br>";

}

    if (isset($_POST['color'])) {

        $color = $_POST['color'];
        $color_string = "";
        for($i=0;$i<count($color);$i++) {

            if($i!=0)  {

                $color_string = $color_string . ", ";

            }

            $color_string = $color_string . $color[$i];

        }

    } else {

        $error .= "Please select a color from the dropdown menu.<br>";

    }

...

$emailTo = "me@mydomain.com";

$subject = "Mat Order";

$content = $_POST['color'];
$content .= $_POST['size'];
$content .= $_POST['monogram']

对于大量代码感到抱歉,只是尝试粘贴相关区域。任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

作为上述插件的替代方案,下面是一个jQuery代码片段,它将预处理并提交表单。同样如上面的评论中所述,请确保在表单和提交按钮中添加“操作”。

<form method="post" action="pathToYourphp.php">
    <!-- Your form content here -->
    <button id="submitBtn" class="btn btn-primary" type="submit">Submit</button>
</form>

jQuery代码:

<script>
$(document).ready(function() {
    // This will replace the text in the dropdown with the item selected from the list
    $(".dropdown-menu li").click(function(){
        $(this).parents(".dropdown").find('.dropdown-toggle').html($(this).text() + ' <span class="caret"></span>');
        $(this).parents(".dropdown").find('.dropdown-toggle').val($(this).data('value'));
    });
    // Handle the submit button click
    $('#submitBtn').click(function(event) {
        // prevent the form from submitting
        event.preventDefault();
        // Get the form to submit
        var form = $(this).closest('form');
        // Add hidden inputs dynamically to the form
        // and fill them with the correct value
        // This will grab the "name" attribute from
        // your button
        $('.dropdown-toggle').each(function() {
            var input = $("<input>")
           .attr("type", "hidden")
           .attr("name", $(this).attr("name"))
           .val($(this).text().trim());
            form.append($(input));
        });
        //submit the form
        form.submit();
    });
});
</script>

自从使用bootstrap以来,你应该有jQuery可用。