2015-05-09 85 views
0

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

<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

+0

值属性会显示您的网页上的按钮的名称是不是?它不被视为像其他领域一样的价值。但是你可以做到这一点 –

+0

不,在按钮上按照传统的方式使用value属性来传递一个值。这是因为按钮以传统方式使用 Chris

+1

以防万一您还没有阅读此内容.http://stackoverflow.com/questions/4007942/jquery-serializearray-doesnt-include-the-smit- button-that-was-0123ed –

回答

1

这是为了做这件事,concatening数据串与特定的点击按钮的名称属性:

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

这将是一个很容易检查,如果你命名提交输入和按钮不同。

您目前有这样的设置是这样的:

<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 

} 
+0

问题是,既不是按钮的值曾经使它到服务器 – Pointy

+0

你好,谢谢,但还有其他问题。变量$ _POST ['submit']没有设置任何方式,所以当我将它重命名为任何其他的,它不会被设置为 – JackDavis

+0

哦,对,对不起 关于那个。在另一个答案中查看关于'.serializeArray()或.serialize()'的建议。我只是处理一个小方面。 – nomistic

3

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

请参阅..

http://api.jquery.com/serialize

http://api.jquery.com/serializeArray

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

0

如果没有事件的功能,那么它不会阻止提交功能,默认情况下得到的将被调用,并且$ _ 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(), 

删除一个提交类型应该只有一个submit类型使它类型的按钮和通话onbutton点击functiuon通过提交阿贾克斯将与提交一样工作。

+0

这是关于Firefox不使用'window.event'模型的好处,但我不知道这是如何回答问题?! –

+0

你好,谢谢,但结果是一样的。在这种情况下应该有什么区别? – JackDavis

+0

无论如何,一切看起来不错,做一些改变和检查 –

相关问题