如何在 jQuery 插件的步骤之间执行 MySQL 更新查询?

发布于 2024-12-17 11:24:53 字数 12153 浏览 0 评论 0原文

我正在使用一个名为 Stepy 的 jQuery 插件,它基于 FormToWizard 插件,允许用户完成 10 步 形式。

由于表单相当长,我希望在每个步骤之间保存用户的数据(即单击“后退”或“下一步”按钮时),以便他们可以保存工作并在稍后返回想要。

我正在使用 PHP 和 MySQL。表行是在用户第一次访问表单时创建的,因此此后的所有执行都将是“更新”查询。

关于我应该如何执行此操作以及在哪里添加功能有什么想法?

JavaScript:

$(function() {

    $('#form').stepy({
        backLabel:  'Back',
        block:      true,
        errorImage: true,
        nextLabel:  'Next',
        titleClick: true,
        validate:   false,
        legend:     false,
    });

});

stepy.js

;(function($) {

    var methods = {
        init: function(options) {
            return this.each(function() {

                var opt     = $.extend({}, $.fn.stepy.defaults, options),
                    $this   = $(this).data('options', opt),
                    id      = $this.attr('id');

                if (id === undefined) {
                    id = 'stepy-' + $this.index();
                    $this.attr('id', id); 
                }

                var $titlesWrapper = $('<ul/>', { id: id + '-titles', 'class': 'stepy-titles' });

                if (opt.titleTarget) {
                    $(opt.titleTarget).html($titlesWrapper);
                } else {
                    $titlesWrapper.insertBefore($this);
                }

                if (opt.validate) {
                    $this.append('<div class="stepy-error"/>');
                }

                var $steps      = $this.children('fieldset'),
                    $step       = undefined,
                    $legend     = undefined,
                    description = '',
                    title       = '';

                $steps.each(function(index) {
                    $step = $(this);

                    $step
                    .addClass('step')
                    .attr('id', id + '-step-' + index)
                    .append('<p id="' + id + '-buttons-' + index + '" class="' + id + '-buttons"/>');

                    $legend = $step.children('legend');

                    if (!opt.legend) {
                        $legend.hide();
                    }

                    description = '';

                    if (opt.description) {
                        if ($legend.length) {
                            description = '<span>' + $legend.html() + '</span>';
                        } else {
                            $.error(id + ': the legend element of the step ' + (index + 1) + ' is required to set the description!');
                        }
                    }

                    title = $step.attr('title');
                    title = (title != '') ? '<div>' + title + '</div>': '--';

                    $titlesWrapper.append('<li id="' + id + '-title-' + index + '">' + title + description + '</li>');

                    if (index == 0) {
                        if ($steps.length > 1) {
                            methods.createNextButton.call($this, index);
                        }
                    } else {
                        methods.createBackButton.call($this, index);

                        $step.hide();

                        if (index < $steps.length - 1) {
                            methods.createNextButton.call($this, index);
                        }
                    }
                });

                var $titles = $titlesWrapper.children();

                $titles.first().addClass('current-step');

                var $finish = $this.children('.finish');

                if (opt.finishButton) {
                    if ($finish.length) {
                        var isForm      = $this.is('form'),
                            onSubmit    = undefined;

                        if (opt.finish && isForm) {
                            onSubmit = $this.attr('onsubmit');
                            $this.attr('onsubmit', 'return false;');
                        }

                        $finish.click(function(evt) {
                            if (opt.finish && !methods.execute.call($this, opt.finish, $steps.length - 1)) {
                                evt.preventDefault();
                            } else {
                                if (isForm) {
                                    if (onSubmit) {
                                        $this.attr('onsubmit', onSubmit);
                                    } else {
                                        $this.removeAttr('onsubmit');
                                    }

                                    var isSubmit = $finish.attr('type') == 'submit';

                                    if (!isSubmit && (!opt.validate || methods.validate.call($this, $steps.length - 1))) {
                                        $this.submit();
                                    }
                                }
                            }
                        });

                        $finish.appendTo($this.find('p:last'));
                    } else {
                        $.error(id + ': element with class name "finish" missing!');
                    }
                }

                if (opt.titleClick) {
                    $titles.click(function() {
                        var array   = $titles.filter('.current-step').attr('id').split('-'), // TODO: try keep the number in an attribute.
                            current = parseInt(array[array.length - 1], 10),
                            clicked = $(this).index();

                        if (clicked > current) {
                            if (opt.next && !methods.execute.call($this, opt.next, clicked)) {
                                return false;
                            }
                        } else if (clicked < current) {
                            if (opt.back && !methods.execute.call($this, opt.back, clicked)) {
                                return false;
                            }
                        }

                        if (clicked != current) {
                            methods.step.call($this, (clicked) + 1);
                        }
                    });
                } else {
                    $titles.css('cursor', 'default');
                }

                $steps.delegate('input[type="text"], input[type="password"]', 'keypress', function(evt) {
                    var key = (evt.keyCode ? evt.keyCode : evt.which);

                    if (key == 13) {
                        evt.preventDefault();

                        var $buttons = $(this).parent().children('.' + id + '-buttons');

                        if ($buttons.length) {
                            var $next = $buttons.children('.button right-aligned');

                            if ($next.length) {
                                $next.click();
                            } else {
                                var $finish = $buttons.children('.finish');

                                if ($finish.length) {
                                    $finish.click();
                                }
                            }
                        }
                    }
                });

                $steps.first().find(':input:visible:enabled').first().select().focus();
            });
        }, createBackButton: function(index) {
            var $this   = this,
                id      = this.attr('id'),
                opt     = this.data('options');

            $('<a/>', { id: id + '-back-' + index, href: 'javascript:void(0);', 'class': 'button left-aligned', html: opt.backLabel }).click(function() {
                if (!opt.back || methods.execute.call($this, opt.back, index - 1)) {
                    methods.step.call($this, (index - 1) + 1);
                }
            }).appendTo($('#' + id + '-buttons-' + index));
        }, createNextButton: function(index) {
            var $this   = this,
                id      = this.attr('id'),
                opt     = this.data('options');

            $('<a/>', { id: id + '-next-' + index, href: 'javascript:void(0);', 'class': 'button right-aligned', html: opt.nextLabel }).click(function() {
                if (!opt.next || methods.execute.call($this, opt.next, index + 1)) {
                    methods.step.call($this, (index + 1) + 1);
                }
            }).appendTo($('#' + id + '-buttons-' + index));
        }, execute: function(callback, index) {
            var isValid = callback.call(this, index + 1);

            return isValid || isValid === undefined;
        }, step: function(index) {
            index--;

            var $steps = this.children('fieldset');

            if (index > $steps.length - 1) {
                index = $steps.length - 1;
            }

            var opt = this.data('options');
                max = index;

            if (opt.validate) {
                var isValid = true;

                for (var i = 0; i < index; i++) {
                    isValid &= methods.validate.call(this, i);

                    if (opt.block && !isValid) {
                        max = i;
                        break;
                    }
                }
            }

            $steps.hide().eq(max).show();

            var $titles = $('#' + this.attr('id') + '-titles').children();

            $titles.removeClass('current-step').eq(max).addClass('current-step');

            if (this.is('form')) {
                var $fields = undefined;

                if (max == index) {
                    $fields = $steps.eq(max).find(':input:enabled:visible');
                } else {
                    $fields = $steps.eq(max).find('.error').select().focus();
                }

                $fields.first().select().focus();
            }

            if (opt.select) {
                opt.select.call(this, max + 1);
            }

            return this;
        }, validate: function(index) {
            if (!this.is('form')) {
                return true;
            }

            var $step   = this.children('fieldset').eq(index),
                isValid = true,
                $title  = $('#' + this.attr('id') + '-titles').children().eq(index),
                opt     = this.data('options'),
                $this   = this;

            $($step.find(':input:enabled').get().reverse()).each(function() {

                var fieldIsValid = $this.validate().element($(this));

                if (fieldIsValid === undefined) {
                    fieldIsValid = true;
                }

                isValid &= fieldIsValid;

                if (isValid) {
                    if (opt.errorImage) {
                        $title.removeClass('error-image');
                    }
                } else {
                    if (opt.errorImage) {
                        $title.addClass('error-image');
                    }

                    $this.validate().focusInvalid();
                }
            });

            return isValid;
        }
    };

    $.fn.stepy = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist!');
        } 
    };

    $.fn.stepy.defaults = {
        back:           undefined,
        backLabel:      '&lt; Back',
        block:          false,
        description:    true,
        errorImage:     false,
        finish:         undefined,
        finishButton:   true,
        legend:         true,
        next:           undefined,
        nextLabel:      'Next &gt;',
        titleClick:     false,
        titleTarget:    undefined,
        validate:       false,
        select:         undefined
    };

})(jQuery);

I am using a jQuery plugin called Stepy, which is based of the FormToWizard plugin, to allow users to complete a 10-step form.

Since the form is fairly long, I'd like the user's data to be saved between each step (i.e. when clicking on the 'back' or 'next' buttons) so they can save their work and come back at a later day if they'd like.

I am using PHP and MySQL. The table row is created for a user when they access the form for the first time, so all executions after that would be 'update' queries.

Any thoughts on how I should go about doing this and where I'd add the functionality in?

Javascript:

$(function() {

    $('#form').stepy({
        backLabel:  'Back',
        block:      true,
        errorImage: true,
        nextLabel:  'Next',
        titleClick: true,
        validate:   false,
        legend:     false,
    });

});

stepy.js

;(function($) {

    var methods = {
        init: function(options) {
            return this.each(function() {

                var opt     = $.extend({}, $.fn.stepy.defaults, options),
                    $this   = $(this).data('options', opt),
                    id      = $this.attr('id');

                if (id === undefined) {
                    id = 'stepy-' + $this.index();
                    $this.attr('id', id); 
                }

                var $titlesWrapper = $('<ul/>', { id: id + '-titles', 'class': 'stepy-titles' });

                if (opt.titleTarget) {
                    $(opt.titleTarget).html($titlesWrapper);
                } else {
                    $titlesWrapper.insertBefore($this);
                }

                if (opt.validate) {
                    $this.append('<div class="stepy-error"/>');
                }

                var $steps      = $this.children('fieldset'),
                    $step       = undefined,
                    $legend     = undefined,
                    description = '',
                    title       = '';

                $steps.each(function(index) {
                    $step = $(this);

                    $step
                    .addClass('step')
                    .attr('id', id + '-step-' + index)
                    .append('<p id="' + id + '-buttons-' + index + '" class="' + id + '-buttons"/>');

                    $legend = $step.children('legend');

                    if (!opt.legend) {
                        $legend.hide();
                    }

                    description = '';

                    if (opt.description) {
                        if ($legend.length) {
                            description = '<span>' + $legend.html() + '</span>';
                        } else {
                            $.error(id + ': the legend element of the step ' + (index + 1) + ' is required to set the description!');
                        }
                    }

                    title = $step.attr('title');
                    title = (title != '') ? '<div>' + title + '</div>': '--';

                    $titlesWrapper.append('<li id="' + id + '-title-' + index + '">' + title + description + '</li>');

                    if (index == 0) {
                        if ($steps.length > 1) {
                            methods.createNextButton.call($this, index);
                        }
                    } else {
                        methods.createBackButton.call($this, index);

                        $step.hide();

                        if (index < $steps.length - 1) {
                            methods.createNextButton.call($this, index);
                        }
                    }
                });

                var $titles = $titlesWrapper.children();

                $titles.first().addClass('current-step');

                var $finish = $this.children('.finish');

                if (opt.finishButton) {
                    if ($finish.length) {
                        var isForm      = $this.is('form'),
                            onSubmit    = undefined;

                        if (opt.finish && isForm) {
                            onSubmit = $this.attr('onsubmit');
                            $this.attr('onsubmit', 'return false;');
                        }

                        $finish.click(function(evt) {
                            if (opt.finish && !methods.execute.call($this, opt.finish, $steps.length - 1)) {
                                evt.preventDefault();
                            } else {
                                if (isForm) {
                                    if (onSubmit) {
                                        $this.attr('onsubmit', onSubmit);
                                    } else {
                                        $this.removeAttr('onsubmit');
                                    }

                                    var isSubmit = $finish.attr('type') == 'submit';

                                    if (!isSubmit && (!opt.validate || methods.validate.call($this, $steps.length - 1))) {
                                        $this.submit();
                                    }
                                }
                            }
                        });

                        $finish.appendTo($this.find('p:last'));
                    } else {
                        $.error(id + ': element with class name "finish" missing!');
                    }
                }

                if (opt.titleClick) {
                    $titles.click(function() {
                        var array   = $titles.filter('.current-step').attr('id').split('-'), // TODO: try keep the number in an attribute.
                            current = parseInt(array[array.length - 1], 10),
                            clicked = $(this).index();

                        if (clicked > current) {
                            if (opt.next && !methods.execute.call($this, opt.next, clicked)) {
                                return false;
                            }
                        } else if (clicked < current) {
                            if (opt.back && !methods.execute.call($this, opt.back, clicked)) {
                                return false;
                            }
                        }

                        if (clicked != current) {
                            methods.step.call($this, (clicked) + 1);
                        }
                    });
                } else {
                    $titles.css('cursor', 'default');
                }

                $steps.delegate('input[type="text"], input[type="password"]', 'keypress', function(evt) {
                    var key = (evt.keyCode ? evt.keyCode : evt.which);

                    if (key == 13) {
                        evt.preventDefault();

                        var $buttons = $(this).parent().children('.' + id + '-buttons');

                        if ($buttons.length) {
                            var $next = $buttons.children('.button right-aligned');

                            if ($next.length) {
                                $next.click();
                            } else {
                                var $finish = $buttons.children('.finish');

                                if ($finish.length) {
                                    $finish.click();
                                }
                            }
                        }
                    }
                });

                $steps.first().find(':input:visible:enabled').first().select().focus();
            });
        }, createBackButton: function(index) {
            var $this   = this,
                id      = this.attr('id'),
                opt     = this.data('options');

            $('<a/>', { id: id + '-back-' + index, href: 'javascript:void(0);', 'class': 'button left-aligned', html: opt.backLabel }).click(function() {
                if (!opt.back || methods.execute.call($this, opt.back, index - 1)) {
                    methods.step.call($this, (index - 1) + 1);
                }
            }).appendTo($('#' + id + '-buttons-' + index));
        }, createNextButton: function(index) {
            var $this   = this,
                id      = this.attr('id'),
                opt     = this.data('options');

            $('<a/>', { id: id + '-next-' + index, href: 'javascript:void(0);', 'class': 'button right-aligned', html: opt.nextLabel }).click(function() {
                if (!opt.next || methods.execute.call($this, opt.next, index + 1)) {
                    methods.step.call($this, (index + 1) + 1);
                }
            }).appendTo($('#' + id + '-buttons-' + index));
        }, execute: function(callback, index) {
            var isValid = callback.call(this, index + 1);

            return isValid || isValid === undefined;
        }, step: function(index) {
            index--;

            var $steps = this.children('fieldset');

            if (index > $steps.length - 1) {
                index = $steps.length - 1;
            }

            var opt = this.data('options');
                max = index;

            if (opt.validate) {
                var isValid = true;

                for (var i = 0; i < index; i++) {
                    isValid &= methods.validate.call(this, i);

                    if (opt.block && !isValid) {
                        max = i;
                        break;
                    }
                }
            }

            $steps.hide().eq(max).show();

            var $titles = $('#' + this.attr('id') + '-titles').children();

            $titles.removeClass('current-step').eq(max).addClass('current-step');

            if (this.is('form')) {
                var $fields = undefined;

                if (max == index) {
                    $fields = $steps.eq(max).find(':input:enabled:visible');
                } else {
                    $fields = $steps.eq(max).find('.error').select().focus();
                }

                $fields.first().select().focus();
            }

            if (opt.select) {
                opt.select.call(this, max + 1);
            }

            return this;
        }, validate: function(index) {
            if (!this.is('form')) {
                return true;
            }

            var $step   = this.children('fieldset').eq(index),
                isValid = true,
                $title  = $('#' + this.attr('id') + '-titles').children().eq(index),
                opt     = this.data('options'),
                $this   = this;

            $($step.find(':input:enabled').get().reverse()).each(function() {

                var fieldIsValid = $this.validate().element($(this));

                if (fieldIsValid === undefined) {
                    fieldIsValid = true;
                }

                isValid &= fieldIsValid;

                if (isValid) {
                    if (opt.errorImage) {
                        $title.removeClass('error-image');
                    }
                } else {
                    if (opt.errorImage) {
                        $title.addClass('error-image');
                    }

                    $this.validate().focusInvalid();
                }
            });

            return isValid;
        }
    };

    $.fn.stepy = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist!');
        } 
    };

    $.fn.stepy.defaults = {
        back:           undefined,
        backLabel:      '< Back',
        block:          false,
        description:    true,
        errorImage:     false,
        finish:         undefined,
        finishButton:   true,
        legend:         true,
        next:           undefined,
        nextLabel:      'Next >',
        titleClick:     false,
        titleTarget:    undefined,
        validate:       false,
        select:         undefined
    };

})(jQuery);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-12-24 11:24:53

看到Stepy页面,您可以在下一个和返回上添加一些功能。因此,您可以使用ajax发送数据并将其保存在MySQL数据库中。例如:

$('#myForm').stepy({
  //when back, save the data
  back: function(index) {
    $.post ...
  },
  next: function(index) {
    $.post ...
  }
});

Seeing Stepy page, you can add some function to the next and back. So, you can send the data with ajax and save this in MySQL database. For example:

$('#myForm').stepy({
  //when back, save the data
  back: function(index) {
    $.post ...
  },
  next: function(index) {
    $.post ...
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文