【问题标题】:(button || input) type=submit - missing value in $_POST variable in jQuery AJAX(按钮 || 输入) type=submit - jQuery AJAX 中 $_POST 变量中的缺失值
【发布时间】:2015-05-09 15:04:38
【问题描述】:

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

<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');
        }
    });
});

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

【问题讨论】:

  • value 属性将在您的网页上显示按钮的名称?不是吗?它不像其他字段那样被视为值。但你可以做到
  • 不,在按钮上,value 属性以传统方式使用,如用于传递值。这是因为按钮以传统方式使用
  • 以防万一您还没有阅读此内容..stackoverflow.com/questions/4007942/…
  • 当心,设置 name="submit" 你正在覆盖默认的 DOM submit() 方法。就是说,我很确定这个问题是一个骗子:stackoverflow.com/questions/4007942/… BTW,似乎与使用 FORM 的 submit 事件更相关,而不是点击事件。如果点击例如email,你真的想向服务器发送数据吗???
  • @JackDavis 实际上,您可以这样处理:jsfiddle.net/26gdp7en 但是恕我直言,您最好针对请求登录或“忘记密码”@987654325 定位不同的服务器脚本@

标签: javascript php jquery html ajax


【解决方案1】:

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

参考..

http://api.jquery.com/serialize

http://api.jquery.com/serializeArray

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

【讨论】:

    【解决方案2】:

    这是一种方法,将数据字符串与特定的单击按钮名称属性连接:

    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');
            }
        });
    });
    

    但更好的方法是根据单击的按钮来定位特定的服务器端脚本,例如:

    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】:

      如果您以不同的方式命名提交输入和按钮,检查会容易得多。

      您目前的设置如下:

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

      尝试将按钮的名称更改为:

      name="forgot"
      

      然后您可以对其进行检查,例如

      if (isset($_POST['submit'])){
      
          stuff here
      
      }
      

      并单独检查

      if (isset($_POST['forgot'])){
      
          stuff here
      
      }
      

      【讨论】:

      • 问题是按钮值都没有到达服务器。
      • 您好,谢谢,但还有其他问题。变量 $_POST['submit'] 未以任何方式设置,因此当我将其重命名为任何其他变量时,它也不会被设置。
      • 哦,对了。对于那个很抱歉。请参阅其他答案中有关 .serializeArray() or .serialize() 的建议。我只解决了一个小方面。
      【解决方案4】:

      如果函数中没有事件,那么它不会阻止提交函数,默认情况下会调用 get 并且 $_POST 肯定会为空 改变

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

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

      再做一次改变

      data: values,
      

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

      删除一种提交类型应该只有一种提交类型使其成为按钮类型并调用onbutton单击功能通过ajax提交它将与提交相同。

      【讨论】:

      • 这是一个关于 Firefox 不使用 window.event 模型的好点,但我不确定这如何回答问题?!
      • 您好,谢谢,但结果是一样的。在这种情况下应该有区别吗?
      猜你喜欢
      • 2016-01-23
      • 2011-05-14
      • 1970-01-01
      • 2018-05-27
      • 2022-09-23
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      相关资源
      最近更新 更多