如何处理表单中的多个提交?

如何处理表单中的多个提交?

问题描述:

I'm trying to put two submit buttons into one form, one button will update a record and another button will delete it.

<form>
   <input type="submit" value="UPDATE"/>
   <input type="submit" value="DELETE/>
</form>

How these should be handled in php? What's the best way to do it?

我正在尝试将两个提交按钮放入一个表单中,一个按钮将更新记录而另一个按钮将删除 它。 p>

 &lt; form&gt; 
&lt; input type =“submit”value =“UPDATE”/&gt; 
&lt; input type =“submit”value =  “DELETE /&gt; 
&lt; / form&gt; 
  code>  pre> 
 
 

如何在php中处理这些内容?最好的方法是什么? p> DIV>

You can just add a name:

<form>
   <input type="submit" name="action" value="UPDATE"/>
   <input type="submit" name="action" value="DELETE"/>
</form>

and use $_GET['action'] or $_POST['action'] (depending if you use get or post for form).

if($_POST['action'] == 'DELETE'){
   //.....
} elseif($_POST['action'] == 'UPDATE'){
   //.....
}

Javascript should do these work. For example:

<script>
function update() {
//what you want to do
}

function del() {
}
</script>

...    

<input type="submit" value="UPDATE" onclick="update();"/>
<input type="submit" value="DELETE  onclick="del();"/>

Use JS :

<form id="theForm" action="foo.php">
...
<input type="submit" value="UPDATE" id="first"/>
   <input type="submit" value="DELETE" id="second"/>    
</form>​

$(document).ready(function() {
    $("#theForm input").click(function(e) {
        e.preventDefault();
        if(e.target.id == 'first') {
            $("#theForm").attr("action", "updatePage.php");
        } else {
            $("#theForm").attr("action", "deletePage.php");
        }
        alert($("#theForm").attr("action"));
        $("#theForm").submit(); 
    });
​});​

Read more about JQuery

I recommend you to use JS for this but still if you want to use only PHP then refer Mihai Iorga's answer.

Hope it'll help you.

There is no issue: In your php code just check which button is clicked and process the logic, for example: if user clicked on DELETE the value of UPDATE will be null, also, if user clicked on UPDATE the value for DELETE will be null, now, consider that your real from looks like this:

<form action="update-delete.php" method="POST">
   <input type="text" name="visibleid" value="<?php echo $id; ?>" disabled="disabled">
   <input type=hidden" name="hiddenid" value="<?php echo $id; ?">
   <!-- other fields will be here -->
   <input type="submit" name="update" value="UPDATE"/>
   <input type="submit" name="delete" value="DELETE"/>
</form>

Note: I used 2 fields for mysql id for that row (record), one for showing it to user, and one for unique identification that you need it when updating and deleting the row.

Now, the PHP file looks like this: I used $_POST because the form method is set to POST, if you not set this attribute or using GET instead, you have to use $_GET ($update = $_GET['update'])

//use $action for identifying button type
$action = '';
if($update == 'UPDATE'){
 $action = 'update';
}
elseif($delete == 'DELETE'){
 $action = 'delete'
}else{
 $action = 'unknown';
}

//now lets start your logic
if($action == 'update'){
 $query = " UPDATE TABLE `tablename` SET `col1` = '$col1' WHERE `id` = '$id' ";
}
elseif($action == 'delete'){
 $query = " DELETE FROM `tablename` WHERE `id` = '$id'";
}
else{
 //echo "";
}
?>