使用Javascript从html FORM发送帖子数据?

Ton*_*uck 5 html javascript forms post

我正在尝试从空行动中发送帖子数据,并使用javascript将信息发送到必须处理帖子信息的文件.

这是我的代码:

<HTML>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
            <script>    
    function post_to_url(path, params, method) {
        method = method || "post";

        var form = document.createElement("form");

        form._submit_function_ = form.submit;

        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);
        form._submit_function_();
    }
    post_to_url("./postinfo.php", { submit: "submit" } );
        </script>   
<form method="post" onsubmit="return post_to_url()">
<input type="text" name="ime">
<input type="submit" id="submit" name="submit" value="Send">
</form>
Run Code Online (Sandbox Code Playgroud)

那么我如何postinfo.php使用html表单中的空行动将发布数据发送到javascript?

提前致谢!

Ric*_*eck 9

你很幸运,因为有一个名为" Ajax " 的JQuery函数可以为你处理这个问题!

您需要做的就是调用JQuery,并使用如下代码:

$('#form_id').on('submit', function(e){
    e.preventDefault();
    $.ajax({
       type: "POST",
       url: "/postinfo.php",
       data: $(this).serialize(),
       success: function() {
         alert('success');
       }
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 大声笑好吧 - 我希望它现在对你有用:)考虑到我的代码实际上有效,你会好心投票并接受答案吗? (2认同)