php - 如何使用 jQuery/AJAX 在主窗体中提交 iframe 子窗体?

标签 php jquery mysql jquery-plugins iframe

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

使用 next/back 属性,我添加了一个功能来在步骤之间发布数据,以便用户可以保存他们的工作并在以后需要时返回。

我的一个步骤允许用户将项目添加到 iframe 中的表单(将数据发布到单独的表)。我希望它能正常工作,以便当用户在步骤之间移动时,iframe 中的项目也会发布到它们单独的表中。有没有办法在步骤之间的 iframe 中提交表单(即在主表单提交时提交 iframe 子表单)?

我正在使用 PHP 和 MySQL。

您能提供的任何帮助都会很棒!

Javascript:

$(function() {

    $('#custom').stepy({
        backLabel:  'Back',
        block:      true,
        errorImage: true,
        nextLabel:  'Next',
        titleClick: true,
        validate:   false,
        legend:     false,
        back:       function(index) {
                         $.post('../../process.php')
                    }
        next:       function(index) {
                         $.post('../../process.php')
                    }
    });

});

HTML:

<html>
  <body>
    <form id="custom" name="custom">

      <fieldset title="Thread 1">
        <legend>description one</legend>
        <label>Question A:</label> 
        <input type="text" id="question_a" name="question_a" class="required">
        <label>Question B:</label> 
        <input type="text" id="question_b" name="question_b">
      </fieldset>

      <fieldset title="Thread 2">
        <legend>description two</legend>
        <iframe src="../../list_form.php" width="100%" height="300"></iframe>
      </fieldset>

      <fieldset title="Thread 3">
        <legend>description three</legend>
        <label>Question C:</label> 
        <input type="text" id="question_c" name="question_c" class="required">
      </fieldset>

      <input type="submit" class="finish" value="Finish!">

    </form>
  </body>
</html>

内嵌框架

<html>
  <body>
    <form id="sub_form" name="sub_form">

        <label>Question 1:</label> 
        <input type="text" id="question_1" name="question_1">
        <label>Question 2:</label> 
        <input type="text" id="question_2" name="question_2">

    </form>
  </body>
</html>

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);

最佳答案

如果您想将 Text\HTML 或任何其他数据附加到您的 iframe(调用同一域中的页面!),您可以使用:

jQuery("#iframe_id").contents().find('body').append('<div>Hello World</div>');

完整示例: Full Example

如果您的 iframe 在另一个域上,您将不得不使用 window.postMessage,您可以在 Mozilla 的文档中阅读:

Mozilla's docs

或者看一下 my blog post关于这个主题。

希望对你有帮助,

关于php - 如何使用 jQuery/AJAX 在主窗体中提交 iframe 子窗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8275176/

相关文章:

PHP 命名空间,已经在命名空间中?

php - 在 Windows 上使用 PHP 进行串行通信

javascript - 如何将模板数据导入Ajax版Smart Admin主题?

MySQL:按值对记录进行计数,然后用其计数更新另一列

mysql - SQL SELECT 最后一个条目没有限制

php - 使用IP地址在谷歌地图上定位用户位置

javascript - jquery ajax跨站

mysql - h2 数据库按位操作(&)语法无效 MySql

jquery - Asp.net mvc 分页机制

javascript - 在 Jquery 中将文本元素更改为图像