我正在使用表单,我有三个提交按钮。编辑,删除和重置但删除和重置按钮不起作用。
我做错了吗?
echo '<form action="" method="post">
<input type="text" name="login" value="'.htmlspecialchars($object['login']).'" placeholder="Login" /><input type="text" name="password" value="'.htmlspecialchars($object['password']).'" placeholder="Password" />
<input type="submit" value="Edit" />
<input type="hidden" name="action" value="edit" />
<input type="hidden" name="id" value="'.$object['id'].'" />
<input type="submit" value="Delete" />
<input type="hidden" name="action" value="dell" />
<input type="submit" value="Reset" />
<input type="hidden" name="action" value="res" />
</form>;
在PHP中它看起来像
if($_POST){
$action=(string)$_POST['action'];
$id=(int)$_POST['id'];
$name=(string)$_POST['name'];
$name=trim($_POST['name']);
$name=htmlspecialchars($name,ENT_QUOTES);
if($action=='add' && $name){
mysql_query("INSERT INTO `stages` SET
`name`='".mysql_real_escape_string($name)."'");
} else if($action=='edit' && $name){
mysql_query("UPDATE `stages` SET
`name`='".mysql_real_escape_string($name)."'
WHERE `id`='".$id."' LIMIT 1");
} else if($action=='edit'){
mysql_query("DELETE FROM `stages` WHERE `id`='".$id."' LIMIT 1");
mysql_query("DELETE FROM `questions` WHERE `stage_id`='".$id."'");
} else if($action=='dell'){
mysql_query("DELETE FROM `stages` WHERE `id`='".$id."' LIMIT 1");
mysql_query("DELETE FROM `questions` WHERE `stage_id`='".$id."'");
} else if($action=='reset'){
mysql_query("UPDATE `stages` SET
`name`=NULL
WHERE `id`='".$id."' LIMIT 1");
}
}
答案 0 :(得分:0)
这可能更像是你需要做的工作 - 尽管它没有经过测试。
<?php
echo '
<form id="actions" action="" method="post">
<input type="text" name="login" value="'.htmlspecialchars( $object['login'] ).'" placeholder="Login" />
<input type="text" name="password" value="'.htmlspecialchars( $object['password'] ).'" placeholder="Password" />
<input type="button" value="Edit" data-action="edit" />
<input type="button" value="Delete" data-action="dell" />
<input type="button" value="Reset" data-action="res" />
<input type="hidden" id="id" name="id" value="'.$object['id'].'" />
<input type="hidden" id="action" name="action" />
</form>
<script type="text/javascript">
var col=document.getElementById("actions").querySelectorAll("input[type=\'button\']");
if( col ){
for( var n in col )if( n && col[n] && col[n].nodeType==1 ) col[n].addEventListener( \'click\', function(e){
document.getElementById("action").value=this.dataset.action;
this.parentNode.submit();
}.bind( col[n] ),false );
}
</script>';
if( $_POST ){
$action=(string)$_POST['action'];
$id=(int)$_POST['id'];
$name=(string)$_POST['name'];
$name=trim($_POST['name']);
$name=htmlspecialchars($name,ENT_QUOTES);
if( $action=='add' && $name ){
mysql_query("INSERT INTO `stages` SET `name`='".mysql_real_escape_string($name)."'");
} else if( $action=='edit' && $name ){
mysql_query("UPDATE `stages` SET `name`='".mysql_real_escape_string($name)."' WHERE `id`='".$id."' LIMIT 1");
} else if( $action=='edit' ){
mysql_query("DELETE FROM `stages` WHERE `id`='".$id."' LIMIT 1");
mysql_query("DELETE FROM `questions` WHERE `stage_id`='".$id."'");
} else if( $action=='dell' ){
mysql_query("DELETE FROM `stages` WHERE `id`='".$id."' LIMIT 1");
mysql_query("DELETE FROM `questions` WHERE `stage_id`='".$id."'");
} else if( $action=='reset' ){
mysql_query("UPDATE `stages` SET `name`=NULL WHERE `id`='".$id."' LIMIT 1");
}
}
?>
按钮将调用javascript函数,然后使用适当的操作和其他参数提交表单。