Question Update the values to the table

Hi,

Please see the attached html file. I am trying to update the set of latitude and longitude values a temp table after the function call 'codeAddress'
Def temp-table tfzipcodes
FIELD zipcode AS char
FIELD LatDest AS DEC /* var lat */
FIELD LongDest AS DEC /* var lng */

Please tell me how can I do that . With some code examples.

TIA
Philip
 

Attachments

  • test.txt
    1.7 KB · Views: 6

Cecil

19+ years progress programming and still learning.
Hi. I think I done what you have requested but it's not the most ideal method. It's probably best wrap-up this kind of processing on the server via PhantonJS/cURL/wget but that a whole different kettle of fish.

I've extend your original html code to use jQuery to perform a AJAX POST for each query response to WebSpeed Brokers. On the WebSpeed side I stored the Temp-tables in to a simple XML file for demonstration purposes. In reality I would be saving it to a database.

Code:
<script language="speedscript">

def var zipcode as char.

Def temp-table tfzipcodes
    FIELD zipcode AS char
    FIELD LatDest  AS DEC
    FIELD LongDest AS DEC
    field NameDest AS CHAR.

&GLOBAL-DEFINE xcXMLFile tfzipcodes.xml
  
PROCEDURE output-header:

    /** HANDLE A POST/AJAX REQUEST  **/
    IF REQUEST_METHOD EQ 'POST' THEN
    DO:
          
        IF SEARCH('{&xcXMLFile}') NE ? THEN
            TEMP-TABLE tfzipcodes:READ-XML('FILE','{&xcXMLFile}','MERGE',?,?).

        FIND tfzipcodes
            WHERE tfzipcodes.zipcode = GET-VALUE('zip')
            No-ERROR.

        IF NOT AVAILABLE tfzipcodes THEN
        DO:
            CREATE tfzipcodes.

            ASSIGN
                tfzipcodes.zipcode = GET-VALUE('zip').
        END.

        ASSIGN
            tfzipcodes.LatDest  = DECIMAL(get-value('lat'))
            tfzipcodes.LongDest = DECIMAL(get-value('lng')).

        /** Return somthing to the browers to stop
            webspeed errore aka 'Yellow Screen of Death'.**/

        output-content-type("text/html":U).

        {&OUT} SUBSTITUTE('<p>TT Updated: &1</p>',
                          STRING(ROWID(tfzipcodes))
                          ) SKIP.

        /** Do whatever with the Temp-Table....**/
        TEMP-TABLE tfzipcodes:WRITE-XML('FILE','{&xcXMLFile}', True).
      
    END.

    RETURN.
END PROCEDURE.

/** Only Display the Page if it's a 'GET' request...**/
IF REQUEST_METHOD NE 'GET' THEN
    RETURN.

PROCEDURE geocodes:
  
    create tfzipcodes.
    Assign
      tfzipcodes.zipcode = '32003'.
      
    create tfzipcodes.
    Assign
      tfzipcodes.zipcode = '01730'.
      
   
    For Each tfzipcodes.
      
    
      {&OUT} "codeAddress( '" + tfzipcodes.zipcode  + "');" skip.
  
    end.
  
    RETURN.
END PROCEDURE.

</script>

<html>
  <head>
    <!--Load the jQuery -->
    <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script type="text/javascript">
  
    // jQuery Page Loaded and ready.
    $(function(){
        function codeAddress(zipcode) {
          
            var lat = '';
            var lng = '';
            var address = zipcode;
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address}, function(results, status) { 
                if (status == google.maps.GeocoderStatus.OK) {   
                    lat = results[0].geometry.location.lat();
                    lng = results[0].geometry.location.lng();
                    console.log("lat:" + lat + "-long:" + lng);
  
                } else {   
  
                alert('Geocode was not successful for the following reason: ' + status);
                }
            //alert("lat:" + lat + "long:" + lng);

            // POST BACK THE RESULT SET TO THE WEBSERVER/WEBSPEED
            var param = {'zip':zipcode, 'lat':lat, 'lng':lng};
              
            $.post('`SELFURL`',
                   param,
                   function(htmlResponce){
                        $('#updateStatus').append( '<strong>' + zipcode + '</stong>').append(  htmlResponce );
                   });  
        });
        }

        <!--WSS RUN geocodes IN THIS-PROCEDURE. -->
      });

</script>

</head>
  <body>


  
    <div id="updateStatus">
      
    </div>

  </body>
</html>
 
Last edited:
Top