2012-06-27 106 views
1

我有html表单如何知道点击了哪个按钮?

我使用,而不是<a href="#" onclick="document.aa.submit()">提交按钮

我的代码会解释我的问题越好..

<html> 
<form name="aa" action="aa.php" method="post"> 
<input type="checkbox" name="posts[]" value="1"> 
<input type="checkbox" name="posts[]" value="2"> 
<a href="#" onclick="document.aa.submit()">Delete</a> 
<a href="#" onclick="document.aa.submit()">Move</a> 
</form> 
</html> 

aa.php

<?php 
print_r($_POST); 
?> 

结果

Array ([posts] => Array ([0] => 1 [1] => 2)) 

现在的问题是:

如何知道用户点击删除或移动?

注:

我知道如果我用<input submit>将修复该问题

,但我不能使用某种原因

注2

的问题是如何提交按钮通过php来检测它。

?例如:

Array ([posts] => Array ([0] => 1 [1] => 2 ['submit'=>'delete'])) 

if('delete'){ 
mysql_query("delete from...") 
} 
else{ 
mysql_query("update/move ...."); 
} 
+0

在a元素上使用ID标签?发布id并用php表单检索它。 – Brendan

+0

使用ID标签来区分元素! – alibenmessaoud

+0

可能重复[多个提交按钮在窗体中](http://stackoverflow.com/questions/3540585/multiple-submit-button-in-a-form) – miku

回答

0

你需要作出一些调整的表格,以便邮政值知道哪个选项,您所提交

<form name="aa" action="aa.php" id='aa' method="post"> 
<input type="checkbox" name="posts[]" value="1"> 
<input type="checkbox" name="posts[]" value="2"> 
    <a href="#" onclick="submit_function(this)" rel="delete">Delete</a> 
     <a href="#" onclick="submit_function(this)" rel="move">Move</a> 
     <input id="submit_type" type="hidden" name="task"> 
     </form> 
在JS

使用jQuery

function submit_function(item){ 
    if(item.rel == 'move') 
    { 
    document.getElementById('submit_type').value ='move' 
    } 
    else if(item.rel == 'delete') 
    { 
    document.getElementById('submit_type').value ='delete' 
    } 

    document.getElementById('aa').submit() 

    } 

在PHP

if($_POST['task'] == 'move') 
{ 
//do move stuff 
} 

    if($_POST['task'] == 'delete') 
{ 
//do delete stuff 
} 
1

只有PHP ...没有使用Javascript?如果您使用的是JavaScript,我会建议使用一个按钮来调用一些JavaScript来提交表单而不是链接,但它可能会给您更好的结果,因为您还可以调用一些调用来设置表单中的某些隐藏字段已提交。另外,为什么提交被破坏?

看看下面的页面,懂得如何使用隐藏域。

http://www.tizag.com/htmlT/htmlhidden.php

在后,你应该能够得到隐藏字段,如果用户已经在PHP,而不是HTML提交它,然后做你的计算。

+0

这是一个绝妙的想法,想法是:点击删除按钮,创建一个隐藏的输入(命名为删除)示例:现在我可以知道哪个按钮已被点击php –

+0

如果答案有帮助,请竖起大拇指您。我是StackOverflow的新手,需要声望。 –

0

使用jQuery您甚至可以在你的标签去掉onclick事件,只是添加下面的jQuery。

$(document).ready(function() { 
    $("a").click(function(event) { 
     alert($(this).html()) // Replace this line with the one below for your code 
     // document.aa.submit($(this).html()) 
    }); 
});​ 

$(this).html()将返回点击a标记的innerHTML。例如“删除”或“移动”。然后你可以使用这个在你的PHP来识别点击临客

+0

对不起,我不使用jquery –

+0

惭愧。将完全适合您的问题 –

+0

hhh,我有一个衣服套件我的大小,但我不穿它,因为他们的颜色 –

1

在这里你去:

<html> 
<form name="aa" action="aa.php" method="post"> 
<input type="hidden" name="action" value=""> 
<input type="checkbox" name="posts[]" value="1"> 
<input type="checkbox" name="posts[]" value="2"> 
<a href="#" onclick="document.aa.action='delete';document.aa.submit()">Delete</a> 
<a href="#" onclick="document.aa.action='move';document.aa.submit()">Move</a> 
</form> 
</html> 
+0

这将工作完美,除非我处理数据在一个文件中,不是多个动作文件,请参阅我的解决方案,如果你碰到这个问题 –

+0

很明显,@ codefactor的想法隐藏领域的名字相同,形式的属性不好:)我认为他是关于改变隐藏元素的价值,而不是'

' – Timur

0

SOLUTION:

<script> 
function createInput(action){ 
var input = document.createElement("input"); 
input.setAttribute("type", "hidden"); 
input.setAttribute("name", "action"); 
input.setAttribute("value", action); 
document.getElementById("forsm").appendChild(input); 
document.aa.submit(); 
} 
</script> 

<html> 
<form name="aa" id="forsm" action="aa.php" method="post"> 
<input type="checkbox" name="posts[]" value="1"> 
<input type="checkbox" name="posts[]" value="2"> 
<a href="#" id="delete" onclick="createInput('delete');">Delete</a> 
<a href="#" id="move" onclick="createInput('move');">Move</a> 
</form> 
</html> 

结果

阵列([帖] =>数组([0] => 2)[动作] =>移动)

非常感谢生锈的韦伯,给我这个想法

+0

我认为codefactor的解决方案好得多,因为该字段总是可用。这在某些情况下会很有用。当然,你可以将他的解决方案包装到功能中。另外,'createInput',我认为对于提交表单的函数也不是一个好名字... – Timur

+0

如果我在多个文件中处理数据,但是我使用一个文件来处理数据,我不能使用代码因子的解决方案多个动作文件,以及我使用约12提交按钮,这意味着我将需要创建12个文件! –

+0

'CreateInput'似乎是一个非常通用的名称,可能会重新考虑重命名'SubmitWithHiddenValue'。我通常也会同意Codefactor的解决方案,因为处理整个解决方案只有一个PHP文件容易出错并且不完全健壮。这实际上取决于所采取的行动类型以及制作该计划所需的复杂程度。 –

相关问题