How can PHP code dynamically change the action attribute of a form based on which submit button is clicked?
To dynamically change the action attribute of a form based on which submit button is clicked, you can use JavaScript to set the form's action attribute before submitting the form. You can achieve this by adding an onclick event to each submit button that updates the action attribute accordingly.
<form id="myForm" method="post" action="">
<input type="submit" name="submit1" value="Submit 1" onclick="document.getElementById('myForm').action = 'action1.php';">
<input type="submit" name="submit2" value="Submit 2" onclick="document.getElementById('myForm').action = 'action2.php';">
</form>
Keywords
Related Questions
- How should special characters be handled in PHP echo statements to avoid errors?
- What improvements can be made to the provided PHP code snippets to enhance readability, maintainability, and performance?
- What are the best practices for handling user-inputted content, such as removing specific HTML tags or attributes to prevent XSS attacks?