我有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>
它将解决问题
但由于某种原因我无法使用提交按钮
问题是如何通过php检测它。
示例:
Array ( [posts] => Array ( [0] => 1 [1] => 2 ['submit'=>'delete']) )
if('delete'){
mysql_query("delete from...")
}
else{
mysql_query("update/move ....");
}
答案 0 :(得分:1)
只有PHP?...没有javascript?如果你使用的是javascript,我会建议使用一个调用一些javascript的按钮提交表单而不是链接,它可能会给你更好的结果,因为你也可以调用一些隐藏的字段来设置它提交。另外,为什么提交破损?
请查看以下页面,了解如何使用隐藏字段。
http://www.tizag.com/htmlT/htmlhidden.php
在帖子上你应该能够获得隐藏的字段,就像用户提交了它一样,然后在php而不是html中进行计算。
答案 1 :(得分: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>
答案 2 :(得分: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中使用它来识别点击的链接
答案 3 :(得分:0)
您需要对表单进行一些调整,以便Post值知道您要提交的选项
<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
}
答案 4 :(得分:0)
解决方案:
<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>
结果
数组([posts] =&gt;数组([0] =&gt; 2)[动作] =&gt;移动)