使用javascript提交动态多位置表单

时间:2010-07-08 19:11:02

标签: javascript forms post events

我正在尝试为我的网站编写一些JavaScript,它将选择页面上的所有表单,为提交时添加事件监听器,然后将提交的值路由到主操作页面和另一个备用页面。

这是我到目前为止所拥有的:

Send.php:

<script type="text/javascript">


    function post_to_url(path, params, method) {
        method = method || "post"; // Set method to post by default, if not specified.

        // The rest of this code assumes you are not using a library.
        // It can be made less wordy if you use one.
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("action", path);

        for(var key in params) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
        }

        document.body.appendChild(form);    // Not entirely sure if this is necessary
        form.submit();
    }

    function addEvent(elm, evType, fn, useCapture) {
        if (elm.addEventListener) {
            elm.addEventListener(evType, fn, useCapture);
            return true;
        }
        else if (elm.attachEvent) {
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        }
        else {
            elm['on' + evType] = fn;
        }
    }

    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        }
        else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }

    function addSubmits() {
        var fs=document.forms;
        for(var i=0;i<fs.length;i++){
            //addEvent(fs[i],"submit",post_to_url('http://www.gwtwg.com/recieve.php', {'email' : 'test.com'}), false)
        }
    }

    addLoadEvent(addSubmits);
</script>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <input type="text" name="email" />
    <input type="submit" value="Submit!" />
</form>

现在脚本只是冻结,但如果我替换:“post_to_url('http://www.gwtwg.com/recieve.php',{'email':'test.com'})” 有: “警报(” foobar的 “)”

我在加载页面时以及提交表单时收到提醒。 (只有在提交按钮时才会出现。)

关于什么错误的想法?

1 个答案:

答案 0 :(得分:1)

所以,这里的问题实际上是你没有使用任何JS库。我个人建议您使用jQuery,但任何主要的都可以使用。您展示的所有函数在各种JS库中具有更强大的等价物,因此当您可以免费替换经过更好测试,更强大的版本时,尝试调试它们是愚蠢的。

如果你正在使用jQuery,那么(我认为)你可以做你想做的事情:

var URL = "http://www.gwtwg.com/recieve.php"; // Define your site's URL
$(function() { // setup an onReady (similar to onLoad) handler
    $("form") // get all forms on the page
        .submit(function() { // hook up an onSubmit handler to them
            var params = $(this).serialize(); // get this form's data
            $.post(URL, paprams);// POST the data your server
      });
});

现在,我还没有真正测试过它,但它应该可以工作(即使它没有,希望你能得到这个想法)。另外,请注意它缩短了多少?又一个好处。重要的是,如果您使用已建立的代码,已经被数百个眼球审查过,您将不会花时间重新发明轮子。此外,更重要的是,你不会浪费时间在互联网上乞讨人们来帮助你调试那个轮子的版本; - )