Community
Showing results for 
Search instead for 
Do you mean 
Reply

JQuery and PHP basic implementation sample

New Member
Posts: 8
Country: United_Kingdom

JQuery and PHP basic implementation sample

I have been implementing a lookup into our quoting platform to pull certain data from ACT using the Act.API... 

 

For anyone looking to start a basic implementation the below may be of interest as it has worked for me. 

 

A Basic JQuery function to process data from ACT API. 

 

This function creates a popup dialog box when the user clicks a div with an id of lookup. The user then inputs the GUID and clicks okay. The Jquery function then requests a token,  then requests the JSON string from the ACT API. It then parses the selected strings to variables and posts them to a php file which writes them to the MySQL database.  

 

IMPORTANT! -- One thing to note is that this function currently exposes the username and password for the act DB to the client pc which could expose your act database.  In our deployment this function is called from a protected webpage and php only exposes the jquery code if the user is already logged in and fills in there username and password for them.

 

$('#lookup').click( function(){
var $q_id = $('#quote_no').attr('title');
    // Dialog here
    $('<form><h2>Enter Company GUID</h2><br><input type="text" style="z-index:10000" name="GUID"><br></form>').dialog({
        modal: true,
        buttons: {
            'OK': function () {
                		var $GUID = $('input[name="GUID"]').val();
            			// get token for api
                		jQuery.extend({
   					 			getToken: function(url) {
       							var username = "USERNAME-HERE";
       							var password = "PASSWORD-HERE";
       							var database = "DB_NAME_HERE";
        						var result = null;
        						$.ajax({
           							url: url,
            						type: 'GET',
            						headers: {
                        			Authorization: "Basic " + btoa(username + ":" + password),
                        			"Act-Database-Name": database
                    					}, 
            						async: false,
            						success: function(data) {result = data;}
        							});
       							return result;
    							}
						});
						var token = $.getToken("API ROOT HERE/act.web.api/" + "/authorize");
            			
            		// get data from API
					jQuery.extend({
    					getValues: function(url) {
        				var result = null;
        				$.ajax({
            				url: url,
        					dataType: "json",
            				type: 'GET',
            				headers: {"Authorization": "Bearer " + token},
            				async: false,
            				success: function(data) {result = data;}
        					});
       						return result;
    						}
						});
					var json = $.getValues("API ROOT HERE/act.web.api/api/companies/" + $GUID);
					console.log(json);
					// String Data Required
					var $address1 = json.address.line1;
					var $address2 = json.address.line2;
					var $address3 = json.address.line3;
					var $postcode = json.address.postalCode;
					var $cusname = json.name;
					var $phone = json.phone;
            	   // post data to php-db handler
            		$.ajax({
                 		context: this,
                		type:"POST",
                 		url:"PHP FILE FOR PROCESSING THE DATA TO MYSQL",
                 		data:{q_id:$q_id, cus_name:$cusname, cus_address:$address1, cus_address2:$address2, cus_address3:$address3, cus_pc:$postcode, cus_tel:$phone,},
                 		success:function(result){}
                	});

                $(this).dialog('close');
            	reload_page()
            },
                'Cancel': function () {
                $(this).dialog('close');
            }
        }
    });

  
	});

 

Administrator
Posts: 3,600
Country: United_Kingdom

Re: JQuery and PHP basic implementation sample

Hi Mark,
Thanks for posting this useful example!