0

I have multiple input fields, like

  <p>Filter by gender</p>
  <select class="filter-users">
     <option value="female">Female</option>
     <option value="male">Male</option>
   </select>
   <p>Filter by city</p>
      <input class="filter-users" type="text" name="city">

How to create GET request url with these one or all of these inputs

$.ajax({
  url: "/users/filter",
  type: "get", 
  data: { 
    city: city, 
    gender: gender
  },
  success: function(response) {

  }
});

How to make ajax request every time on of the filter inputs changed? For example send new request when on option change, or text input was entered? So what I am trying to explain is, there is no submit button. Requests needs to be made everytime one of these fields changes

4
  • 1
    multiple input forms or multiple input fields? Commented Aug 22, 2017 at 15:47
  • I don't get the question, you're already passing in both of your input values Commented Aug 22, 2017 at 15:49
  • do you want to get the value from your "ddl", and then pass it? Commented Aug 22, 2017 at 15:50
  • @Amit multiple fields Commented Aug 23, 2017 at 7:35

2 Answers 2

1

You can use serializeArray, like this:

$.ajax({
  url: "/users/filter",
  type: "get", 
  data: $("form").serializeArray(),
  success: function(response) {
  }
});

Or you also can use serialize in case you don't need JSON but URL-encoded parameters.

Sign up to request clarification or add additional context in comments.

Comments

0

for this "How to make ajax request every time on of the filter inputs changed"-

$('input.filter-users').on('change', function(){
   $.ajax({
     url: "/users/filter",
     type: "get", 
    data: $("form").serializeArray(),
    success: function(response) {
       }
   });
});

1 Comment

@Amit for select use as $('.filter-users').on('change', function(){., the above answer for input type field,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.