0

Hello all i want to append parameter to url if and only if search text box value is different than default. Actually i have 3 search text boxes and i want to append them to url

here is my code..

$("#search-btn").click(function (e) {
    e.preventDefault();
    var search_country = $("#search-country").val();
    var search_city = $("#search-city").val();
    var search_team = $("#search-team").val();
    var dataString = 'action=search'

    // var dataString = {
    //   action : 'search',
    //   param1 : $("#search-country").val() || '', 
    //   param2 : $("#search-city").val() || '',
    //   param3 : $("#search-team").val() || ''
    // };

    if (search_country != "Search by Country") {
        dataString = dataString + "&param1=" + search_country
    }
    if (search_city != "Search by City") {
        dataString = dataString + "&param2=" + search_city
    }
    if (search_team != "Search by team") {
        dataString = dataString + "&param3=" + search_team
    }
    $.ajax({
        type: "get",
        url: "SearchServlet",
        data: dataString,
        success: function (data) {
            $('body').html(data);
            $('#msg').html('Search Results')
        }
    });

});

I want to pass parameter only if user enter search criteria otherwise not....

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Developer Desk
  • 2,294
  • 8
  • 36
  • 77

2 Answers2

2

try this:

$("#search-btn").click(function (e) {
    e.preventDefault();
    var search_country = $("#search-country").val();
    var search_city = $("#search-city").val();
    var search_team = $("#search-team").val();
    var dataString =  {};

    dataString["action"] = "search";

    if (search_country != "Search by Country") {
        dataString["param1"] = search_country;
    }
    if (search_city != "Search by City") {
        dataString["param2"] = search_city;
    }
    if (search_team != "Search by team") {
        dataString["param3"] = search_team;
    }
    $.ajax({
        type: "get",
        url: "SearchServlet",
        data: dataString,
        success: function (data) {
            $('body').html(data);
            $('#msg').html('Search Results')
        }
    });

});
Hugo Tostes
  • 412
  • 4
  • 13
2

Last I recall, jQuery will take a JavaScript parameter object and convert it into the GET queryString parameters. Just prepare your object

$("#search-btn").click(function (e) {
  e.preventDefault();
  var search_country = $("#search-country").val();
  var search_city = $("#search-city").val();
  var search_team = $("#search-team").val();

  var params = {
    action : 'search'
  };

  if (search_country != "Search by Country") {
    params.param1 = search_country
  }
  if (search_city != "Search by City") {
    params.param2 = search_city
  }
  if (search_team != "Search by team") {
    params.param2 = search_team
   }
  $.ajax({
    type: "get",
    url: "SearchServlet",
    data: params,
    success: function (data) {
        $('body').html(data);
        $('#msg').html('Search Results')
    }
  });

});
cbayram
  • 2,259
  • 11
  • 9