我无法提交表格。我哪里错了?
<div>
<form id = "search_form" action = "/dashboard/" method = "post">
<span class = "pull-right">
Search : <input type = "text" placeholder = "Search" id = "search">
</span>
</form>
</div>
的jQuery
$("#search_form").submit(function(event) {
event.preventDefault();
var formdata = $(this).serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "gallery.php",
data: formdata,
success: function(){alert('success');}
});
});
提交后,流程不会进入jQuery函数。为什么呢?
答案 0 :(得分:2)
您没有为Search
字段指定名称,因此您没有向gallery.php
文件传递任何值,这可能会导致服务器端错误,因为它需要一个值。
<div>
<form id="search_form" action="/dashboard/" method="post">
<span class="pull-right">
Search : <input type="text" placeholder="Search" id="search" name="search">
</span>
</form>
</div>
注意,我已将字段命名为search
,您需要检查PHP文件从POST数据接收的变量名称并相应地命名。
答案 1 :(得分:1)
尝试:
$(function() {
$("#search_form").submit(function(event) {
event.preventDefault();
var formdata = $(this).serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "gallery.php",
data: formdata,
success: function(){alert('success');}
});
});
});