i want to use javascript to submit my form to the backend. However, I have to add a variable parameter to the form url/action endpoint as well. Wondering what the javascript way of doing this is?
This is what i have now
<form id="profile-message-form" action="/send-message?to" method="get">
<textarea name="message-content" id="message-content" maxlength="1000" contentEditable="true"></textarea>
<div id="send-button-container">
<button id="profile-message-submit" onclick="sendMessage('profile-message-form')" name="subject" type="submit"><i id="profile-message-send-icon" class="fas fa-paper-plane"></i><p>Send</p></button>
</div>
</form>
function sendMessage(form_id) {
var form = document.getElementById(form_id);
// find email address to send to
// submit form with email address as ":to" req param
form.submit();
}
The easiest way is to add hidden fields to form before submitting it.
const formEl = document.getElementById('form');
formEl.onsubmit = e => {
e.preventDefault();
const hiddenAgeEl = document.createElement('input');
hiddenAgeEl.type = 'hidden';
hiddenAgeEl.name = 'age';
hiddenAgeEl.value = 22;
formEl.append(hiddenAgeEl);
formEl.submit();
}
<form id="form" action="/send-message?to" method="get">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>