QueryTask with 2 user inputs Javascript API

1847
3
08-19-2010 05:57 AM
MartynShields
New Contributor II
Hi All

I'm new to ESRI and Javascript and i'm a little stuck.

How do I get a queryTask to run with 2 user inputs?

I'm looking at having 2 textboxes and one onclick button, but I want both user inputs to be used in the same query.

Here is my code with one input, I think that the code that I have bolded need to be altered but I can't seem to get it to work.


                //build query task
        queryTask = new esri.tasks.QueryTask("http://arcgis/ArcGIS/rest/services/AllIncidents/MapServer/0");

        //build query filter
        query = new esri.tasks.Query();
        query.returnGeometry = true;
        query.outFields = ["ADDRESS1", "ADDRESS2", "STATIONGROUND"];


        //create the infoTemplate to be used in an InfoWindow.
        //All ${attributeName} will be substituted with the attribute value for current feature.
        infoTemplate = new esri.InfoTemplate("${ADDRESS1}", "Address1 : ${ADDRESS1}<br/> Address2 : ${ADDRESS2}<br />station : ${STATIONGROUND}");

  //create symbol for selected features
  symbol = new esri.symbol.SimpleMarkerSymbol();
  symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
  symbol.setSize(10);
  symbol.setColor(new dojo.Color([0,255,0,0.25]));

      }

   function executeQueryTask(station) {
        //set query based on what user typed in for population;

 
   query.where = "Upper(ADDRESS1) LIKE '"+ "%" + station.toUpperCase() + "%" +  "'" ;

                                ;
  //execute query and call showResults on completion
        queryTask.execute(query,showResults);
      }

function showResults(featureSet) {
        //remove all graphics on the maps graphics layer
        map.graphics.clear();

        //QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.

  //Performance enhancer - assign featureSet array to a single variable.
  var resultFeatures = featureSet.features;

        for (var i=0, il=resultFeatures.length; i<il; i++) {
          //Get the current feature from the featureSet.
          //Feature is a graphic
          var graphic = resultFeatures;
          graphic.setSymbol(symbol);

          //Set the infoTemplate.
          graphic.setInfoTemplate(infoTemplate);

          //Add graphic to the map graphics layer.
          map.graphics.add(graphic);
        }
      }

      dojo.addOnLoad(init);
    </script>
  </head>
  <body class="tundra">
  <br/>
Please Enter Incident Address: <input type="text" id="station" value="a164 rd" />

<input type="button" value="Get Details" onclick="executeQueryTask(dojo.byId('station').value);"
/>


<div id="mapDiv" style="width:600px; height:600px; border:1px solid #000;"></div>
Click on a city once it's highlighted to get an InfoWindow.
0 Kudos
3 Replies
DonCaviness
New Contributor III
Try changing your query.where statement to this unless you have a field called Upper(ADDRESS1):

query.where = "ADDRESS1 LIKE '%" + station.toUpperCase() + "%'" ;

Also you have an extra space in the onclick event:
onclick="executeQueryTask(dojo.byId('station').val ue);"

Change it to:
onclick="executeQueryTask(dojo.byId('station').value);"

I hope this helps.
0 Kudos
MartynShields
New Contributor II
Try changing your query.where statement to this unless you have a field called Upper(ADDRESS1):

query.where = "ADDRESS1 LIKE '%" + station.toUpperCase() + "%'" ;

Also you have an extra space in the onclick event:
onclick="executeQueryTask(dojo.byId('station').val ue);"

Change it to:
onclick="executeQueryTask(dojo.byId('station').value);"

I hope this helps.


Hi Don

Thanks for getting back to me.

My query and code works fine as it is, the UPPER is to handle users entering data in the wrong case, and the extra space is just how the code was formatted as bold.

The issue I have is altering my working code to include 2 user inputs via 2 text boxes but only using one button to execute the query.
0 Kudos
DonCaviness
New Contributor III
Sorry about that, I must have misread you post.  It looks like you are passing a value from one of the text boxes.  Try this to include the values from both.

query.where = "Upper(ADDRESS1) LIKE '%" + station.toUpperCase() + "%' AND <AttributeName> LIKE '%" +  dojo.byId('txtBox2').value + "%'";

This is the way I created a query string from two different user inputs.
0 Kudos