我会尽力为您翻译中文。以下是需要翻译的内容:
我试图从没有动作的表单发送帖子数据,并使用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>
我该如何使用JavaScript将POST数据发送到postinfo.php
,并在HTML表单中保留空操作?
提前致谢!
post_to_url
- 一次是在定义函数后,另一次是在表单提交时。我怀疑这可能会引起问题。 - Matthew