Page 1 of 1

Redirect to view/edit page if existing data available (JS AJAX Call)

Posted: 2020-12-05 12:12
by sathukorala
I will explain with an example
This is the use case
1.png
1.png (38.74 KiB) Viewed 3123 times
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
If the user has permission he can edit, unless he can view only

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;
?>