-1
<form action="/search" method="post" id="search-form">
    <input type="search" name="search">
    <input type="submit" value="Search">
</form>

<script>
    $("#search-form").submit(function(event) {
      event.preventDefault();
    })
</script>

When I am submitting the form and getting the values in with PostFormValue. When clicking on submit button it reloads the whole page. I just want to avoid that!!!

1 Answers1

2

You can use formData object then send it via axios or fetch functions.

<form action="/search" method="post" id="search-form">
    <input type="search" name="search">
    <input type="submit" value="Search">
</form>
    
<script>
    $("#search-form").submit(function(event) {
        event.preventDefault();
        let formData = new FormData();
    
        $.each($(this).serializeArray(), function (key, input) {
            formData.append(input.name, input.value);
        });
        axios.post("/url", formData).then(() => /* do something*/);
    })
    
</script>
ozdemir
  • 379
  • 5
  • 8