简体   繁体   English

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

[英](button || input) type=submit - missing value in $_POST variable in jQuery AJAX

I have login form where are two buttons - "login" and "forgot password?" 我有登录表单,其中有两个按钮-“登录”和“忘记密码”? And I need to check what button user clicked. 我需要检查用户单击了什么按钮。

<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) says: var_dump($ _ POST)说:

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

I am trying both ways (input type=submit and button type=submit) but none of them send the "submit" value. 我正在尝试两种方式(输入类型=提交和按钮类型=提交),但是它们都不发送“提交”值。

(I am using jquery ajax) (我正在使用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');
        }
    });
});

Please do you know where the problem can be? 请您知道问题可能在哪里吗? I know, there are lot of threads about value of button but nothing works for me. 我知道,关于按钮的价值有很多线程,但是对我来说没有用。 I also tried this example: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_value2 我也尝试了以下示例: http : //www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_value2

The .serializeArray() or .serialize() method uses the standard W3C rules for successful controls to determine which elements it should include; .serializeArray()或.serialize()方法使用标准的W3C规则来成功控制控件,以确定它应包括哪些元素; in particular the element cannot be disabled and must contain a name attribute. 特别是该元素不能被禁用,并且必须包含name属性。 No submit button value is serialized since the form was not submitted using a button. 由于没有使用按钮提交表单,因此没有序列化提交按钮的值。 Data from file select elements is not serialized. 来自文件选择元素的数据未序列化。

Refer.. 参考..

http://api.jquery.com/serialize http://api.jquery.com/serialize

http://api.jquery.com/serializeArray http://api.jquery.com/serializeArray

jQuery serializeArray doesn't include the submit button that was clicked jQuery serializeArray不包含单击的提交按钮

This is one way to do it, concatening data string with specific clicked button name attribute: 这是一种实现方法,将数据字符串与特定的单击按钮名称属性进行连接:

HTML: HTML:

<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">
    <button type="button" name="login" class="submit">Login</button>
    <button type="button" name="forgot" class="submit">Forgot password?</button>
</form>

JQ: JQ:

$("#loginForm").on('click', '.submit', function (event) {

    /* Stop form from submitting normally */
    event.preventDefault();

    /* Get some values from elements on the page: */
    var values = $(this).closest('form').serialize() + '&' + this.name;
    console.log(values);
    /* 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');
        }
    });
});

But better would be to target specific server side script depending which button is clicked, eg: 但是最好是根据单击哪个按钮来定位特定的服务器端脚本,例如:

HTML: HTML:

<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">
    <button type="button" name="login" class="submit" data-url="login.php">Login</button>
    <button type="button" name="forgot" class="submit" data-url="forgot.php">Forgot password?</button>
</form>

JQ: JQ:

$("#loginForm").on('click', '.submit', function (event) {

    /* Stop form from submitting normally */
    event.preventDefault();

    /* Get some values from elements on the page: */
    var values = $(this).closest('form').serialize();
    /* Send the data using post and put the results in a div */
    $.ajax({
        url: $(this).data('url'),
        /* 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');
        }
    });
});

It will be a lot easier to check if you name the submit input and the button differently. 如果您以不同的方式命名提交输入和按钮,检查起来会容易得多。

You currently have this set up like this: 您目前的设置如下:

<input type="submit" name="submit" value="Login">
<button type="submit" name="submit" value="Forgot password?">Forgot password?</button>

Try changing the name of the button to something like: 尝试将按钮的名称更改为:

name="forgot"

then you can run a check on it such as 然后您可以对其进行检查,例如

if (isset($_POST['submit'])){

    stuff here

}

and a separate check for 和单独的支票

if (isset($_POST['forgot'])){

    stuff here

}

If there is not event in function then it will not prevent the submit function and by default get will be called and and $_POST will be empty for sure Change 如果函数中没有事件,那么它将不会阻止Submit函数,并且默认情况下将调用get,并且$ _POST将为空,以确保更改

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

To

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

Make one more change 再做一个改变

data: values,

To

data:$("#loginForm").serialize(),

Remove one submit type there should be only one submit type make it type of button and call onbutton click functiuon to submit via ajax it will work same as submit. 删除一种提交类型,应该只有一种提交类型使其成为按钮类型,然后调用onclick单击功能以通过ajax进行提交,其作用与提交相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM