Jquery 不从 html 表单中收集信息
Jquery not collecting info from an html form
我正在尝试使用jquery来运行php文件。php 将一些信息发送到数据库。
php 本身是有效的。我可以删除js代码,所有内容都发送到数据库。从我的数据库可以明显看出发生了什么。未收集表单中的内容。有些东西会进入数据库,但不会进入表单中的信息。
这是我的代码,可能有什么问题?
// submit without refresh
$(function() {
$('#form').submit(function(e) {
// Stop the form actually posting
e.preventDefault();
// Send the request
$.post('submit.php', {}, function(d) {
console.log(d);
// Here we handle the response from the script
// We are just going to alert the result for now
alert(d);
});
});
});
我的表单有id="form"
为什么 js 不收集表单信息?
您不能以这种方式发布表单。 $.post 将不知道表单的元素,除非您告诉它要发送什么。为了使上面的代码正常工作,您必须将表单的数据传递给$.post,例如:
$.post('submit.php',
data: { <your form data here> },
success: function(data){
console.log(data);
}
);
您需要将字段值传递给 php 脚本:
$.post('submit.php', {data: $(this).serialize()}, function(d) {
console.log(d);
// Here we handle the response from the script
// We are just going to alert the result for now
alert(d);
});
您必须先获取表单数据。序列化用于获取表单数据。
$(function() {
$('#form').submit(function(e) {
var data = $(this).serialize(); // get the form datas
// Stop the form actually posting
e.preventDefault();
// Send the request
$.post('submit.php', data, function(d) {
console.log(d);
// Here we handle the response from the script
// We are just going to alert the result for now
alert(d);
});
});
});
发送数据的最佳方法是 ajax 方法,如下所示:
var nameVar = $("#name").val();
var loc = $("#location").val();
$.ajax({
type: "POST",
url: "some.php",
data: { name: nameVar, location: loc
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
相关文章:
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 获取html表单信息并使用ajax将其推送到PHP
- 试图用cookie中的信息填充表单
- 使用node-js上传文件,并将表单信息存储在数据库(mysql)中
- 根据选择选项元素将表单重定向到不同的URL,并在电子邮件中获取表单详细信息
- 通过javascript提示获取表单的信息
- 采用HTML表单,将信息传递给Python脚本
- 如何获取给定表单详细信息的嵌套json对象
- 将信息从表单传递到表单Javascript
- HTML5,CSS3,PHP联系人表单不发送信息+添加必填字段
- 制作超链接 同时提交一些表单信息
- 提交表单,使用脚本处理信息,并在同一页面上显示结果
- 使用 javascript 从表单提供的信息连接到 MySQL
- 如何在 html5 sql 数据库中存储表单信息
- 创建可重复的 HTML 表单并从 JS 数组加载信息
- 将多个重复的表单与不同的信息发布到新页面
- 在jquery中通过$.ajax提交表单后获取成功信息
- 输入最后一个字段后自动提交/刷新信息路径表单
- Jquery 不从 html 表单中收集信息
- 简单的javascript:数组和表单信息收集