0

ブラーでDBを更新するこのjQueryがあります。コードが更新された後に現在の DB 値を表示することを除いて、正常に動作します。データベースには値があり、フィールドの金額がそれに追加され、更新されます。これを確認する唯一の方法は、何かが更新されるたびにページを更新することです。

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript">
    // JQUERY: Plugin "autoSumbit"
    (function($) {
        $.fn.autoSubmit = function(options) {
            return $.each(this, function() {
                // VARIABLES: Input-specific
                var input = $(this);
                var column = input.attr('name');

                // VARIABLES: Form-specific
                var form = input.parents('form');
                var method = form.attr('method');
                var action = form.attr('action');

                // VARIABLES: Where to update in database
                var where_val = form.find('#where').val();
                var where_col = form.find('#where').attr('name');

                // ONBLUR: Dynamic value send through Ajax
                input.bind('blur', function(event) {
                    // Get latest value
                    var value = input.val();
                    // AJAX: Send values
                    $.ajax({
                        url: action,
                        type: method,
                        data: {
                            val: value,
                            col: column,
                            w_col: where_col,
                            w_val: where_val
                        },
                        cache: false,
                        timeout: 10000,
                        success: function(data) {
                            // Alert if update failed
                            if (data) {
                                document.getElementById("notice").innerHTML="Error, NO UPDATE";
                            }
                            // Load output into a P
                            else {
                                $('#notice').text('Updated');
                                $('#notice').fadeOut().fadeIn();
                            }
                        }
                    });
                    // Prevent normal submission of form
                    return false;
                })
            });
        }
    })(jQuery);
    // JQUERY: Run .autoSubmit() on all INPUT fields within form
    $(function(){
        $('#ajax-form INPUT').autoSubmit();
    });
    </script>

HTMLのもの

            <label>Total:</label>
            <input name="company" value="<?php echo $row['total'] ?>" />

        <label>Tax-in:</label>
            <input name="lastname" value="<?php echo $row['taxin'] ?>" />
4

1 に答える 1

1

PHP ファイルが行をデータベースに挿入した後、データベースから新しい値を SELECT し、jQuery への AJAX 応答でエコー バックします。次に、jQuery を使用して、その新しい値を任意の場所に入力します。

于 2012-08-24T15:25:57.203 に答える