【发布时间】:2019-07-11 19:10:26
【问题描述】:
为了读取具有多个提交选项的多个复选框的值,我使用以下代码:
<form action="" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="move" value="Move"/>
<input type="submit" name="copy" value="Copy"/>
</form>
每次提交都应该执行不同的操作,我的 php 看起来像这样:
if($_POST['delete']) {
if(isset($_POST['check_list'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
// code for delete goes here
echo 'Files are deleted!';
}
}
}
if($_POST['move']) {
if(isset($_POST['check_list'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
//code for moving files goes here
echo 'Files are moved!';
}
}
}
if($_POST['copy']) {
if(isset($_POST['check_list'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
// code for copy goes here
echo 'Files are copied!';
}
}
}
这对我来说很好。 我想要实现的目标:我想将提交的内容放在网站上完全不同的位置。 如下所示:
<form action="" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
</form>
<!-- some code goes here -->
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="move" value="Move"/>
<input type="submit" name="copy" value="Copy"/>
我怎样才能做到这一点?
顺便说一句:我正在使用 ajax 进行发布操作
【问题讨论】:
-
将 type="submit" 更改为 type="button" 为每个按钮提供 id ,在单击事件时,您可以序列化表单数据并通过 ajax 发布
-
你能给出一小段示例代码吗?我的意思是,对于点击事件和序列化
标签: php forms checkbox submit form-submit