我正在尝试从空行动中发送帖子数据,并使用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>
那么我如何使用我的html表单中的空操作javascript将帖子数据发送到postinfo.php
?
提前致谢!
答案 0 :(得分:9)
你很幸运,因为有一个名为&#34; Ajax&#34;的JQuery函数。为你处理这个!
您需要做的就是调用JQuery,并使用如下代码:
$('#form_id').on('submit', function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "/postinfo.php",
data: $(this).serialize(),
success: function() {
alert('success');
}
});
});
答案 1 :(得分:1)
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" } );
更改为
post_to_url("/postinfo.php", { submit: "submit" } );
答案 2 :(得分:0)
试试这段代码。
<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>
答案 3 :(得分:0)
<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>`
答案 4 :(得分:-2)
__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()