从具有多个按钮的FORM外部获取输入值



我在表格中包含了一个表格,用于收集每一行的ID值,其中有一个复选框,还有一个来自表格外部的按钮,用于从表格外部提交表格。

现在它只适用于一个函数,通过POST提交,但如果我想要多个函数呢?

我不能在表单中包含按钮,因为我有其他功能要放在表单中,并且会与破坏表单冲突。

我的atm代码是:

<html>
<body>
<button type="submit" form="form">Submit</button>
<table>
<thead>
<tr><th>Value</th></tr>
<tr><th>Name</th></tr>
</thead>
<tbody>
<form action="page.php" method="POST" id="form">
<?php
foreach($datas as $d){
echo '<tr><td><input type="checkbox" name="array[] value='.$d["id"].' />'.$d["id"].'</td></tr>';
}
?>
</form> 
</tbody>
</table>

如果我只想提交值,这很好,但如果我想要多个按钮和多个操作,比如这样,从表单外部:

<html>
<body>
<button type="submit" form="form" name="submit">Submit</button>
<button type="submit" form="form" name="delete">Delete</button>
<button type="submit" form="form" name="export">Export</button>
...

然后用将它们收集到page.php中

if (isset($_POST['submit'])) //do submit actions
if (isset($_POST['delete'])) //do delete actions
if (isset($_POST['export'])) //do export actions

我试过这么做,但没有成功,那么,你会怎么解决呢?有什么方法可以做到这一点吗?

实际上,您尝试做的事情没有什么特别的错误。下面是一个示例,它应该演示如何根据在表单外部单击的按钮为单个表单执行多个操作。

<?php
$action = false;
if (filter_input(INPUT_POST, 'submit'))
$action = 'submit';
else if (filter_input(INPUT_POST, 'delete'))
$action = 'delete';
else if (filter_input(INPUT_POST, 'export'))
$action = 'export';

$datas = array(
array('id' => 1),
array('id' => 2),
array('id' => 3)
);
$ids = array();
if ($action) {
$ids = filter_input(INPUT_POST, 'ids', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);
}
?>
<html><head><title>Test</title></head>
<body>
<p>
<?php
switch ($action) {
case 'submit':
echo 'Submitting: ' . implode(', ', $ids);
break;
case 'delete':
echo 'Deleting.: ' . implode(', ', $ids);
break;
case 'export':
echo 'Exporting: ' . implode(', ', $ids);
break;
default:
echo 'Waiting for action.';
break;
} ?>
</p>
<form method="POST" id="form">
<table>
<?php
foreach($datas as $d){
printf('<tr><td><input type="checkbox" name="ids[]" value="%1$d" %2$s />%1$d</td></tr>',
$d["id"],
in_array($d["id"], $ids) ? 'checked="checked"' : '');
}
?>
</table>
</form>

<button type="submit" form="form" name="submit" value="submit">Submit</button>
<button type="submit" form="form" name="delete" value="delete">Delete</button>
<button type="submit" form="form" name="export" value="export">Export</button>
</body>
</html>

我更喜欢使用filter_input,因为它会使您对参数的检查更加一致,并且更容易处理您期望的内容。

更好地使用ajax。

<html>
<body>
<button id="sub">Submit</button>
<button id="del">Delete</button>
<button id="exp">Export</button>
<table>
<thead>
<tr><th>Value</th></tr>
<tr><th>Name</th></tr>
</thead>
<tbody>
<form action="page.php" method="POST" id="form">
<imput type="hidden" name="submit" id="isub" value="0">
<imput type="hidden" name="delete" id="idel" value="0">
<imput type="hidden" name="export" id="iexp" value="0">
<?php
foreach($datas as $d){
echo '<tr><td><input type="checkbox" name="array[] value='.$d["id"].' />'.$d["id"].'</td></tr>';
}
?>
</form> 
</tbody>
</table>
<script>
function saveItem(redirect)
{

$.post(saveUrl, $('#form').serialize(), function () {
window.location.assign(redirect);
});
}

$('#sub').on('click', function (e) {
e.preventDefault();
$('#isub').val(1);
saveItem('page.php');
});
$('#del').on('click', function (e) {
e.preventDefault();
$('#idel').val(1);
saveItem('page.php');
});
$('#exp').on('click', function (e) {
e.preventDefault();
$('#iexp').val(1);
saveItem('page.php');
});
</scrip>

最新更新