Action normally specifies the file/page that the form is submitted to (using the method described in the method paramater (post, get etc.))
An action of # indicates that the form stays on the same page, simply suffixing the url with a #. Similar use occurs in anchors. <a href=#">Link</a> for example, will stay on the same page.
Thus, the form is submitted to the same page, which then processes the data etc.
Answer from MEURSAULT on Stack Overflowwhat do <form action="#"> and <form method="post" action ...
How to submit data from HTML form? - WordPress Development Stack Exchange
is this <form action = "index.html" method = "post"> addressing action
Different ways to submit a form. Which is best for my application?
Videos
Action normally specifies the file/page that the form is submitted to (using the method described in the method paramater (post, get etc.))
An action of # indicates that the form stays on the same page, simply suffixing the url with a #. Similar use occurs in anchors. <a href=#">Link</a> for example, will stay on the same page.
Thus, the form is submitted to the same page, which then processes the data etc.
action="" will resolve to the page's address. action="#" will resolve to the page's address + #, which will mean an empty fragment identifier.
Doing the latter might prevent a navigation (new load) to the same page and instead try to jump to the element with the id in the fragment identifier. But, since it's empty, it won't jump anywhere.
Usually, authors just put # in href-like attributes when they're not going to use the attribute where they're using scripting instead. In these cases, they could just use action="" (or omit it if validation allows).
I agree with other answers that most of the time it is better to use a plugin to handle form submissions. However, there are exceptions to this rule.
If you find yourself in a situation where you need to handle the form submissions yourself, you can submit data to admin-post.php. Here is how to set that up:
First set up your form.
<!-- Submit the form to admin-post.php -->
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="POST">
<!-- Your form fields go here -->
<!-- Add a hidden form field with the name "action" and a unique value that you can use to handle the form submission -->
<input type="hidden" name="action" value="my_simple_form">
</form>
Now you will set up an action in your functions.php file. You will use the value that you used in your hidden action field in the form.
function my_handle_form_submit() {
// This is where you will control your form after it is submitted, you have access to $_POST here.
}
// Use your hidden "action" field value when adding the actions
add_action( 'admin_post_nopriv_my_simple_form', 'my_handle_form_submit' );
add_action( 'admin_post_my_simple_form', 'my_handle_form_submit' );
Here is a link to more information from the codex - https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
Best to put this kind of functionality in a plugin. Not cram it into the theme. You can see an example of a simple contact form plugin for wordpress here: https://www.sitepoint.com/build-your-own-wordpress-contact-form-plugin-in-5-minutes/
Tips:
- Execute your code on the right moment, find the hook which suits you (ea. the init hook).
- use wp_mail() in wordpress, not the standard php mail() function
As @WebElaine mentioned above: when you create a plugin for it you won't lose your functionality when switching themes. Also the functionality will be easy to transfer between sites if needed, and you can turn it on/off with the click of a button. If the functionality is only specific for this website, it's still a good idea no to include it in the (child theme's) functions.php. Only "theme" specific functionality (widgets, including specific styling/js files /etc) should be placed there. ea. You wouldn't include the contact form 7 code in your child theme