[英]submit a form using jquery is not working
我正在尝试使用按钮单击result_submit
事件提交表单。 在这里,我使用type作为button
而不是submit
。
将按钮类型从“提交”更改为“按钮”的原因:我试图在表单提交后显示一些隐藏的div,但是我无法使用提交功能来实现这一点。 当我将其更改为type = button时,我可以使用onclick函数来实现。
我在这里面临的问题是我现在没有任何结果。一旦我将按钮类型更改回submit
,它就会起作用。 有什么建议可以解决这个问题吗?
表格html:
<form method="post" id="form_result" action="">
<----some options to select here->
<div class="form-group">
<button onclick="show_result()" type="button" name="result_submit" id="result_submit" style="display: none;margin:1%;" >Submit</button>
</div>
</form>
jQuery:
function show_result() {
document.getElementById('form_result').submit();
}
php(文件名:functions.php):
if(isset($_POST['result_submit'])){
include_once 'dbConnection.php';
<--some codes here----->
if ($_POST['result_options'] == 'Current_Month'){
<---some codes here----->
}
}
重要的提示:-
我的PHP代码在默认的WordPress functions.php
文件中。 我为表单添加了动作,但是不起作用。
还尝试将PHP代码从functions.php
移至新文件。 那也不起作用。 我认为我的其他代码有问题。
试试这个。 在click函数上,它将获取form_data并序列化表单值
$('#result_submit').click(function () {
var form_data = $("#form_result");
var data = form_data.serialize();
console.log(data );
}
首先,删除style="display: none;margin:1%;"
从您的result_submit
按钮,否则它将不可见。
您的代码可以正常工作,但是问题出在您的php文件functions.php
。
在其中,您正在检查类似if(isset($_POST['result_submit'])){
条件,因此当您使用input type='submit'
时条件返回true ,而当您使用<button>
时返回false ,原因如下。
它不起作用,因为命名输入 type="submit"
也会与其他表单的命名字段一起提交,而命名 type="button"
不会提交 。
在下面的例子中, button
与name='button'
将不会提交 ,而input type=submit
与name='submit'
会得到提交 。
样本form.html
<form action="insert.php" method="POST">
<!-- this won't get submitted despite being named -->
<button type="button" name="button">Normal Button</button>
<!-- this also won't get submitted despite being named -->
<input type="button" name="button1" value="Button1" >
<!-- this one does; so the input's TYPE is important! -->
<input type="submit" name="submit" value="Submit Button">
</form>
因此,如下更改您的functions.php
文件,它将按您期望的那样工作:
if( isset($_POST) && $_POST){
include_once 'dbConnection.php';
//some code here
if (isset($_POST['result_options']) && $_POST['result_options'] == 'Current_Month'){
//some codes here
}
}
它将使用上面相同的代码工作,只需更改您的functions.php文件或提交表单的文件即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.