gpt4 book ai didi

javascript - (按钮 || 输入) type=submit - jQuery AJAX 中 $_POST 变量缺少值

转载 作者:行者123 更新时间:2023-11-28 15:23:37 25 4
gpt4 key购买 nike

我有登录表单,其中有两个按钮 - “登录”和“忘记密码?”我需要检查用户单击了哪个按钮。

<form id="loginForm">
<div class="login-error" id="login-error"></div>
<input type="text" id="email" name="email">
<input type="password" id="password" name="password">
<input type="submit" name="submit" value="Login">
<button type="submit" name="submit" value="Forgot password?">Forgot password?</button>
</form>

var_dump($_POST) 说:

array(2) { ["email"]=> string(0) "" ["password"]=> string(0) "" }

我正在尝试两种方式(输入类型=提交和按钮类型=提交),但它们都没有发送“提交”值。

(我正在使用jquery ajax)

 $("#loginForm").click(function(){
/* Stop form from submitting normally */
event.preventDefault();

/* Get some values from elements on the page: */
var values = $(this).serialize();

/* Send the data using post and put the results in a div */
$.ajax({
url: "login.php", /* here is echo var_dump($_POST); */
type: "post",
data: values,
success: function(data){
$("#login-error").html(data);
},
error:function(){
$("#result").html('There is error while submit');
}
});
});

请问您知道问题出在哪里吗?我知道,有很多关于按钮值(value)的话题,但对我来说没有任何作用。我也尝试过这个例子: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_value2

最佳答案

.serializeArray() 或 .serialize() 方法使用成功控制的标准 W3C 规则来确定应包含哪些元素;特别是该元素不能被禁用并且必须包含名称属性。由于未使用按钮提交表单,因此不会序列化提交按钮值。文件选择元素中的数据未序列化。

引用..

http://api.jquery.com/serialize

http://api.jquery.com/serializeArray

jQuery serializeArray doesn't include the submit button that was clicked

关于javascript - (按钮 || 输入) type=submit - jQuery AJAX 中 $_POST 变量缺少值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com