A form with a "GET" action will grab the form data and format it as query parameters then navigate to the page were the action attribute points to.
This can be emulated easily by hand. Demonstration:
document.querySelector("form")
.addEventListener("submit", (event) => {
//do not navigate automatically
event.preventDefault();
customSubmit(event);
});
function customSubmit(event) {
//get the form data from the submitted form
const form = event.target.closest("form");
const formData = new FormData(form);
//convert to URL search params to handle the data as appropriate
const urlSearchParams = new URLSearchParams(formData);
const payload = urlSearchParams.toString();
console.log("payload:", payload);
//create the destination URL
const destination = new URL(form.action);
destination.search = payload;
console.log("navigating to:", destination.toString());
//do not actually navigate for the demo but this is how it works:
//location.href = destination;
//alternatively to send the data without navigating:
//fetch(destination)
}
<form action="https://websiteurl.com/searchresult" method="get" id="bookingform">
<input type="hidden" name="hidden_field_1" value="val1">
<input type="hidden" name="hidden_field_2" value="val2">
<input id="checkin" type="text" name="checkin" value="h@llo">
<input id="checkout" type="text" name="checkin" value="world$">
<input type="submit" id="search" value="search">
</form>
The only change needed is to add custom encoding:
return urlSearchParams.toString()
//use an encoded =
.replaceAll("=", encodeURIComponent("="))
//use an encoded &
.replaceAll("&", encodeURIComponent("&"));
document.querySelector("form")
.addEventListener("submit", (event) => {
//do not navigate automatically
event.preventDefault();
customSubmit(event);
});
function customSubmit(event) {
//get the form data from the submitted form
const form = event.target.closest("form");
const formData = new FormData(form);
//convert to URL search params to handle the data as appropriate
const urlSearchParams = new URLSearchParams(formData);
//do a custom conversion to also encode the & and = characters
const payload = customConvert(urlSearchParams);
console.log("payload:", payload);
//create the destination URL
const destination = new URL(form.action);
destination.search = payload;
console.log("navigating to:", destination.toString());
//do not actually navigate for the demo but this is how it works:
//location.href = destination;
//alternatively to send the data without navigating:
//fetch(destination)
}
function customConvert(urlSearchParams) {
return urlSearchParams.toString()
//use an encoded =
.replaceAll("=", encodeURIComponent("="))
//use an encoded &
.replaceAll("&", encodeURIComponent("&"));
}
<form action="https://websiteurl.com/searchresult" method="get" id="bookingform">
<input type="hidden" name="hidden_field_1" value="val1">
<input type="hidden" name="hidden_field_2" value="val2">
<input id="checkin" type="text" name="checkin" value="h@llo">
<input id="checkout" type="text" name="checkin" value="world$">
<input type="submit" id="search" value="search">
</form>
This works because there is guarantee that there would be no other = and & characters in the encoded URL search parameters string. If there were any, they would have been encoded already URL encoded:
const searchParams = new URLSearchParams({
"f=oo": "he&llo",
"b&ar": "wo=rld",
});
console.log(searchParams.toString());
=and the data, e.g.foo=h@llo&bar=world$becomesfoo%3Dh%40llo%26bar%3Dworld%24. Or should it be double encoded, so the payload becomesfoo%3Dh%2540llo%26bar%3Dworld%252?