繁体   English   中英

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

[英]html form elements and using php to get data

我已经搜索并找到了一些不错的潜在解决方案,但似乎无法使这项工作奏效。 我还是一个初学者。 我正在使用带有下拉菜单的表单元素,并尝试获取用户输入,然后通过电子邮件发送信息。 我将在此处发布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']

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

作为上述插件的替代方法,下面是一段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>

由于您正在使用引导程序,因此应该有可用的jQuery。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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