Two buttons in a form with different functions
I just wanna ask how can I make a form with two buttons that do different functions? Like for example: I have this table with checkboxes and when a checkbox was checked and a certain button was clicked, the button will perform its assigned task.
Button1 can add, button2 can delete.
Can yo开发者_C百科u help me again, please? I'm kind of new at this and I really want to know. Thank you!
This is more of a JavaScript question than PHP.
You'll need to add an onclick
to your buttons:
<input type="button" onclick="functionA();" value="button a" />
<input type="button" onclick="functionB();" value="button b" />
Then create these functions in JavaScript:
function functionA()
{
// do stuff
alert("add");
}
function functionB()
{
// do stuff
alert("delete");
}
Here's a basic example of where you can stick your JavaScript etc:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<script type="text/javascript">
function functionA()
{
// do stuff
alert("add");
}
function functionB()
{
// do stuff
alert("delete");
}
</script>
</head>
<body>
<input type="button" onclick="functionA();" value="button a" />
<input type="button" onclick="functionB();" value="button b" />
</body>
</html>
If you are looking at a server side solution, you can have two submit buttons in the form with different name attributes and then check the $_GET['buttonName']
or $_POST['buttonName']
variables depending on your form submission method.
For example:
<form action="action.php" method="post">
<input type="text"....blah blah />
....
<input type="submit" name="add" value="Add" />
<input type="submit" name="delete" value="Delete" />
</form>
精彩评论