Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
See the first example in this section: const form = new FormData(document.getElementById('login-form')); fetch('/login', { method: 'POST', body: form }); To adapt this example to your c...
Answer
#2: Post edited
- See the first example in [this section](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body):
- ```js
- const form = new FormData(document.getElementById('login-form'));
- fetch('/login', {
- method: 'POST',
- body: form
- });
- ```
To adapt this example to your code, you'll need to change the URL being provided to `fetch` from `'/login'` to the URL of your PHP behavior page—looks like that might be just `'/behavior.php'` from your example. And of course you'll need to get the correct form DOM object to use in the `FormData` constructor. This example uses an `id` attribute on the form and the `document.getElementById` function to do this, which is a nice way to do it but not the only way.
- See the first example in [this section](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body):
- ```js
- const form = new FormData(document.getElementById('login-form'));
- fetch('/login', {
- method: 'POST',
- body: form
- });
- ```
- To adapt this example to your code, you'll need to change the URL being provided to `fetch` from `'/login'` to the URL of your PHP behavior page—looks like that might be just `'/behavior.php'` from your example. And of course you'll need to get the correct form DOM object to use in the [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) constructor. This example uses an `id` attribute on the form and the `document.getElementById` function to do this, which is a nice way to do it but not the only way.
#1: Initial revision
See the first example in [this section](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body): ```js const form = new FormData(document.getElementById('login-form')); fetch('/login', { method: 'POST', body: form }); ``` To adapt this example to your code, you'll need to change the URL being provided to `fetch` from `'/login'` to the URL of your PHP behavior page—looks like that might be just `'/behavior.php'` from your example. And of course you'll need to get the correct form DOM object to use in the `FormData` constructor. This example uses an `id` attribute on the form and the `document.getElementById` function to do this, which is a nice way to do it but not the only way.