Html 使用Javascript从html FORM发送发布数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19476532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:38:22  来源:igfitidea点击:

Send post data from html FORM with Javascript?

javascripthtmlformspost

提问by Tonny Struck

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.

我正在尝试使用空操作从 from 发送帖子数据,并将带有 javascript 的信息发送到必须处理帖子信息的文件。

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.phpwith javascript with empty action in my html form?

那么如何postinfo.php在我的 html 表单中使用带有空操作的 javascript发送帖子数据呢?

Thanks in advance!

提前致谢!

回答by Richard Peck

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

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

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

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

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

回答by Ningappa

function post_to_url(path, params, method) {
    method = method || "post";

    var form = document.createElement("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" } );

change to

改成

post_to_url("/postinfo.php", { submit: "submit" } );

回答by Andy Dufresne

Try this code.

试试这个代码。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script type="text/javascript">
    function validate() {
        var ra = document.getElementById("uname").value;
        var rag = document.getElementById("pwd").value;
        $.ajax({
       type: "POST",
        url: "/login",
            contentType: "application/json",
         dataType: 'json',
       data:JSON.stringify({
           username:ra,
           password:rag
       }),
       success: function() {
         alert('success');
       }
    });
        console.log(ra, rag)

    }
</script>
<html>
<form name="myform">
<p>ENTER USER NAME <input id="uname" type="text" name="username"></p>

    <p>ENTER PASSWORD <input type="password" id="pwd" name="pwd"></p>
<input type="button" value="Check In" name="Submit" onclick= "validate()">


</form>
</html>

回答by Andy Dufresne

<html>
<form action="{{ url_for('details') }}" method="post">
<p>ENTER NAME<input id="uname" type="text" name="username"></p>

    <p>ENTER DESIGNATION<input type="text" id="pwd" name="pwd"></p>
    <!--<P>ENTER EMAIL-ID</EMAIL-ID><input id="email" type="text" name="email-id"></P>-->
<input type="submit" value="Submit" name="Submit">


</form>
</html>`

回答by Andy Dufresne

 __author__ = 'raghavendra'
from flask import Flask, render_template, request, jsonify
import os
import sys
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
# sys.path.append(('/path/to/django/project'))
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('temp.html')

@app.route('/save',methods=['GET','POST'])
def details():

    a = request.form.get('username')
    b = request.form.get('pwd')
    print a, b

if __name__ == '__main__':
    app.run()