Php 当多个提交不是表单的一部分时,如何处理这些提交
为了读取具有多个提交选项的多个复选框的值,我使用以下代码:Php 当多个提交不是表单的一部分时,如何处理这些提交,php,forms,checkbox,submit,form-submit,Php,Forms,Checkbox,Submit,Form Submit,为了读取具有多个提交选项的多个复选框的值,我使用以下代码: <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"><labe
<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>
C/C++
Java
PHP
每次提交都应执行不同的操作,我的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!';
}
}
}
if($\u POST['delete'])){
if(isset($\u POST['check\u list']){//在提交时运行PHP脚本
如果(!空($\u POST['check\u list'])){
//循环以存储和显示单个复选框的值。
foreach($\u POST['check\u list']作为$selected){
echo$已选定。“”;
}
//删除代码在这里
echo“文件已删除!”;
}
}
}
如果($_POST['move'])){
if(isset($\u POST['check\u list']){//在提交时运行PHP脚本
如果(!空($\u POST['check\u list'])){
//循环以存储和显示单个复选框的值。
foreach($\u POST['check\u list']作为$selected){
echo$已选定。“”;
}
//这里是移动文件的代码
echo“文件已移动!”;
}
}
}
如果($_POST['copy'])){
if(isset($\u POST['check\u list']){//在提交时运行PHP脚本
如果(!空($\u POST['check\u list'])){
//循环以存储和显示单个复选框的值。
foreach($\u POST['check\u list']作为$selected){
echo$已选定。“”;
}
//复制代码在这里
echo“文件已复制!”;
}
}
}
这对我来说很好。
我想要实现的是:我想把提交的内容放在网站上完全不同的地方。
如下所示:
<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"/>
C/C++
Java
PHP
我怎样才能做到这一点
顺便说一下:我正在使用ajax进行后期操作对于这种情况,您必须使用一些jquery:
<script>
$("input[type='submit']").click(function(){
var _form = $("form");
_form.append($("<input/>",{"name":$(this).attr("name")}));
_form.submit();
});
</script>
$(“输入[type='submit']”)。单击(函数(){
var形式=$(“形式”);
_append($(“”,{“name”:$(this.attr(“name”)});
_表单提交();
});
您需要更改“提交到”按钮
<input type="button" class="submit-form" name="delete" value="Delete"/>
<input type="button" class="submit-form" name="move" value="Move"/>
<input type="button" class="submit-form" name="copy" value="Copy"/>
将type=“submit”更改为type=“button”为每个按钮指定id,单击事件时,您可以序列化表单数据并通过Ajax发布。您可以给出一小段示例代码吗?我的意思是,对于点击事件和序列化
<form action="url/goeshere" id="my-form" method="post">
$(".submit-form").click(function(event) {
$form = $("#my-form");
$.post($form.attr("action"), $form.serialize() + "&submit="+
$(this).attr("value"), function(data) {
// do something with response (data)
});