I am working on a project in which there has to be a functionality to permit the users to update and delete certain rows of a table that will dynamically be displayed to them.
The user will click on a radio button to select which row he wants to update or delete and then will click in either the update or submit button.
According to his selection of update or delete, I have to pass the contents of the selected row to 2 a servlet. Now, the servlet for update is different from that of delete. I cannot mention the url pattern in the action attribute of the form as I need the values to be transferred to 2 different servlets according to the users choice.
Is it possible to achieve this?
Please suggest me a few solutions to this problem.
yes, multiple submit buttons can include in the html form. One simple example is given below.
Create another button with type submit. Also add a 'formaction' attribute to this button and give it the value of the secondary URL where you want to send the form-data when this button is clicked. The formaction attribute will override the action attribute of the form and send the data to your desired location.
Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.
Disabling the button or displaying a process overlay are common methods to avoid multiple submits. If you desire to do this server side, think about if you would want the backend api to limit multiple POST requests?
The submit button 's name and value 's attribute will also be POSTED if you click that button to submit the form. In the servlet , you can check if you can get these parameters to know which button is clicked .
For example , suppose you have two buttons , one for update and one for delete
<input type="submit" name="update" value="Update Button">
<input type="submit" name="delete" value="Delete Button">
If the update button is clicked , it will post the variable update=Update Button
If the delete button is clicked , it will post the variable delete=Delete Button
Then in the servlet :
if (request.getParameter("update") != null) {
//update button is clicked
//Do the update action or forward the request to the servlet to do update action
} else if (request.getParameter("delete") != null) {
//delete button is clicked
//Do the delete action or forward the request to the servlet to do delete action
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With