使用Javascript发送来自HTML FORM的发布数据? [英] Send post data from html FORM with Javascript?

查看:108
本文介绍了使用Javascript发送来自HTML FORM的发布数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从空行为发送发布数据,并使用javascript将信息发送到必须处理发布信息的文件。



这是我的代码:

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

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 =postonsubmit =return post_to_url()>
< input type =textname =ime>
< input type =submitid =submitname =submitvalue =Send>
< / form>

所以我可以如何将发布的数据发送到 postinfo.php with javascript with empty action in my html form?



在此先感谢!

解决方案

你很幸运,因为有一个名为 Ajax ,它可以为你处理!



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

('submit',function(e){
e.preventDefault();
$ .ajax({
类型:POST,
url:/postinfo.php,
data:$(this).serialize(),
success:function(){
alert('success');
}
});
});


I am trying to send post data from from with empty action and send the info with javascript to the file that must handle the post information.

Here is my code:

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

So how i can send the post data to postinfo.php with javascript with empty action in my html form?

Thanks in advance!

解决方案

You're quite lucky because there's a JQuery function called "Ajax" which handles this for you!

All you need to do is call JQuery, and use code like this:

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

这篇关于使用Javascript发送来自HTML FORM的发布数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆