Page 1 of 1

Autoload available data when data entering to form

Posted: 2020-12-03 01:37
by sathukorala
Is there a possibility to autoload available data when data entering to top fields

Like in the following example.
1.png
1.png (18.06 KiB) Viewed 3060 times

Re: Autoload available data when data entering to form

Posted: 2020-12-03 19:27
by pbottcher
Hi,

yes you can. Attach a on "change" event handler to the selection box and if the necessary information is available, catch the data via ajax and fill the cases.

Re: Autoload available data when data entering to form

Posted: 2020-12-04 00:46
by sathukorala
pböttcher wrote:
2020-12-03 19:27
Hi,

yes you can. Attach a on "change" event handler to the selection box and if the necessary information is available, catch the data via ajax and fill the cases.
Thanx pböttcher for the idea.
Can you help me a little bit with some elaboration....

Re: Autoload available data when data entering to form

Posted: 2020-12-04 20:20
by pbottcher
Hi,

can you be a bit more specific. Otherwise it is difficult to help further. What kind of dropdown do you have? What field do you need to fill? ....

Re: Autoload available data when data entering to form

Posted: 2020-12-05 01:46
by sathukorala
Thanks and here are the details

Re: Autoload available data when data entering to form

Posted: 2020-12-05 01:48
by sathukorala
pböttcher wrote:
2020-12-04 20:20
Hi,

can you be a bit more specific. Otherwise it is difficult to help further. What kind of dropdown do you have? What field do you need to fill? ....
1.png
1.png (38.74 KiB) Viewed 2930 times
This is the actual page

I have attached the project file too

User selects 1 to 4 and they are all option lists with predefined values
COVID cases is a variable field and it should be loaded when all 1 to 4 are selected and if a record is available user should see the edit page instead of Save New button

Re: Autoload available data when data entering to form

Posted: 2020-12-05 11:57
by sathukorala
I found a solution which works !!!

If above 4 dropdowns are selected and when the "Time Period" dropdown is clicked, AJAX call for the ajax.php with data parsing and if
any matches are found the user prompted to be redirected to the view/edit page

This goes in the hooks/table_name-dv.js

Code: Select all

$j('#time_period').on('click', function(){

		load_details();

});


function load_details(){

	if (($j('#time_period-container').select2('val') == '{empty_value}') || ($j('#province-container').select2('val') == '{empty_value}') || ($j('#district-container').select2('val') == '{empty_value}') || ($j('#hospital-container').select2('val') == '{empty_value}')){
		
		$j('#cases').val('');

	}else{

		time_value = $j('#time_period').val();
		province_value = $j('#province').val();
		district_value = $j('#district').val();
		hospital_value = $j('#hospital').val();

		$j.ajax({
			url: 'hooks/ajax.php',
			data: { time_column : time_value , province_column : province_value , district_column : district_value , hospital_column : hospital_value},
			success: function(data){
				if (!$j.trim(data)) {
					$j('#cases').val('');
				}else{
					alert ("Record Exists !!! Click OK to redirect to the record view page (If you have permission you can edit)");
					window.location = "COVID_cases_view.php?SelectedID="+data;
				}
			}
		});

	}
}
This goes in the hooks/ajax.php

Code: Select all

<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");


	$time = makesafe($_REQUEST['time_column']);
	$province = makesafe($_REQUEST['province_column']);
	$district = makesafe($_REQUEST['district_column']);
	$hospital = makesafe($_REQUEST['hospital_column']);


	$sql = "SELECT id FROM covid_cases WHERE time_period = '{$time}' AND province = '{$province}' AND district = '{$district}' AND hospital = '{$hospital}'";

	$data = sqlValue($sql);

	echo $data;
?>
Experts comment and refine the code