Page 1 of 1

Auto fill a form/fields with data from another table

Posted: 2016-02-12 15:40
by shasta59
Using Appgini 5.50 (I encourage everyone to always state their version.)

As of this post I will no longer reply or assist anyone who does not put the version number near the top of their post. It makes it so much easier to help if you know the version name without having to ask.

(Think of it this way. When you go to buy a car part they always ask for the year/make/model of the car or they cannot help you.)

(note: this was done in an 5.30 generated version but also works in newer versions.)

Lets say you have an app which you are using to track members, book training and more.
You have at least the following two tables: (could be many more)

User profile: used to record details such as name, address, phone etc. I setup this table with other code to limit the ability to create more than one record in the table per user. (They do not need more than one record of their details so why let them create extra records. I also set it up so that they must have this record filled in first before they can do anything else).

Attend a conference: used to register for a conference, meeting etc.

As we all know everyone gets tired of having to put our details in each time on each page but there can be good reasons to require it. (Sorry not getting into those reasons.)

Once you have the user profile table with records you can pull data from that table as follows:

You will need two main files. The first one is the php file to get the data from the server and the user profile table. The second one is a file placed in the hooks folder - tablename-dv.js.

Okay here we go.

Php file first - basic file to get data.

Code: Select all

<?php

$currDir=dirname(__FILE__);
	include("$currDir/lib.php"); //needed for a few things
	include("$currDir/config.php"); //needed to get database variables to log in
	
	$mid = getLoggedMemberID(); //gets member id to use to lookup details
	
	// routine to connect to database - uses variables from config.php
	mysql_connect($dbServer, $dbUsername, $dbPassword) or die(mysql_error()); 
 mysql_select_db($dbDatabase) or die(mysql_error());
 
 
// section to get unique variable from database which identifies the pkValue associated with that user.
	$result1 = mysql_query("SELECT * FROM membership_userrecords WHERE memberID = '$mid' and tableName = 'your_employee_info_tablename'") or die(mysql_error()); //pulls record from the database table
	
while($row1 = mysql_fetch_array($result1)){

$p1 = $row1["pkValue"];

// membership_userrecords is a Appgini created table do not change this name!!!
// your_employee_info_tablename is your created table - change this to match your tablename
}

// section to get employee info from the employee info table
$result2 = mysql_query("SELECT * FROM your_employee_info_tablename WHERE id = '$p1'") or die(mysql_error()); //query to get single record which contains info you wish to use

$info = array(); //variable to hold the array to send data later

while($row2 = mysql_fetch_array($result2)){ //gets data from above query

$f1 = $row2["first_name"];  //field one - I left my field names in place as an example only

$f2 = $row2["last_name"]; //field two - I left my field names in place as an example only

$f3 = ($f1 . " " . $f2); // combines first and last names of employee together - as an example of how to do this

$f4 = $row2["employeeid"]; //some other value - as an example

$info = array('name' => $f3, 'empid' => $f4); // puts records into the array to bring into other table. Assign unique names - I left name and empid in as examples only. You can make up your own.
}
echo json_encode($info); //converts info array into format need to import with javascript code.

?>
Okay, lets move on to the javascript file you need.

You create this as a hook file in your hook folder. See this link for more info on how to name the file correctly. VERY IMPORTANT!
http://www.bigprof.com/appgini/help/adv ... agic-files

The following code is not as tight as it can be since I am sharing it I tend to write it a bit more verbose to aid understanding.

The names I have assigned to various files etc you can change as you wish. The following code is placed in a file which is put in the hooks folder so it runs when the details view is opened. It will not overwrite existing data at all. The first function does the following:

Checks to make sure document loaded fully first.
Checks to see if there is any data in first field you wish to fill. This prevents over writing any data in existing record. In my case I am checking to see if anything in the name field. This is the id of the field as found in the template file.

Code: Select all

document.observe('dom:loaded', function () {
		var x = document.getElementById("name").value; //change name to match the field you wish to check to see if any data present
	if(!x.match(/\S/)){ // checks for content in field
		getdata(); // if no content in the field it runs the next function.
		return true;	
	}
	});

function getdata(){  //this is the referenced function in the above code
	var http_request = new XMLHttpRequest();  
	try{
		// Opera 8.0+, Firefox, Safari
		http_request = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	http_request.onreadystatechange = function(){
		if(http_request.readyState == 4){
			var data = JSON.parse(http_request.responseText);
			
			document.getElementById("name").value = data.name;  //notice these names match the names in the array created in the php file
			document.getElementById("location_of_incident").value = data.empid;
		}
	}
	http_request.open("GET", "empdetails.php",true);  //this gets the info from the php file
	http_request.send();

}
This works fine for me.
I recommend removing all the comments out from your working copy as they are not needed. It will save a few microseconds of data transmit time. My working code has no spaces between lines etc. A few microseconds here and there can really add up.

Enjoy

(I will not reply to requests to explain lines which do not have an explanation attached. They can be looked up in reference guides for php, mysql, javascript etc.)

A little hint: I always test my code using the Incognito or private mode of browsers. This insures no data is cached and then when I make changes I do not have to flush caches. Also, my test server does not allow any caching. Slows it down a bit and creates more drive access but it makes sure I have the most recent code loading at all times during testing.

Alan