如何使用表单发送localstorage

时间:2017-10-05 14:54:40

标签: javascript forms

我有html表单和localstorage

我需要从localstorage发送数据并通过电子邮件发送表单

现在我发两封信:(

当用户提交表格时,我收到两个字母。来自表格的第一封信和来自localstorage的第二封信

所以,我希望收到一封带有localstorage和form

的信

我的表格:

<form action="/send.php" id="form-cart-send" onsubmit="return sendCartdata();" method="post" enctype="multipart/form-data" required>

    <div class="form_fields" data-count="3">
        <div class="form_field" data-type="name" data-is-required="true">
            <label class="field_title">Имя<i>*</i></label>

            <div class="form_field_text">
                <input type="text" class="form_field_text_input" name="name" autocomplete="off" required>

            </div>
        </div>
        <div class="form_field" data-type="phone" data-is-required="1">
            <label class="field_title">Телефон<i>*</i></label>

            <div class="form_field_text">
                <input type="tel" class="form_field_text_input form_phone" id="phone" name="phone" data-check="phone" autocomplete="off" required>

            </div>
        </div>
        <div class="form_field" data-type="email" data-is-required="0">
            <label class="field_title">Ваш e-mail</label>

            <div class="form_field_text">
                <input type="text" class="form_field_text_input" name="email" data-check="email" autocomplete="off" required>

            </div>
        </div>
    </div>

    <div class="form_fields_advanced"></div>

    <div class="form_submit">
        <a class="component-button form_field_submit filled squared" id="checkout" data-modal-id="cart_done">
            <div class="btn-content">
                <div class="form_submit_text">Заказать</div>
            </div>
        </a>
    </div>
</form>

本地存储的脚本:

<script>
    function sendCartdata() {
        var phone = document.getElementById("phone").value;
        if (phone === '') {
            alert("Заполните, пожалуйста, обязательные поля.");
            return false;
        } else {
            setTimeout(function() {
                var value = localStorage.getItem('cart');

                jQuery.post("/cart-send.php", {
                    cart: value
                }, function(data) {

                }).fail(function() {
                    alert("Damn, something broke");
                });
            }, 100);
            return false;
        }
    }
</script>

0 个答案:

没有答案