dongzhiji0814 2015-05-09 15:04
浏览 38
已采纳

(button || input)type = submit - jQuery AJAX中$ _POST变量中的缺失值

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:

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)

 $("#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

  • 写回答

4条回答 默认 最新

  • dougang5993 2015-05-09 16:00
    关注

    This is one way to do it, concatening data string with specific clicked button name attribute:

    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:

    $("#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, e.g:

    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:

    $("#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');
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器