Page 2 of 2

Re: How to Use Ajax OR Retrieve Data from Database IN DynaFo

Posted: Wed Feb 03, 2016 3:36 am
by ashkufaraz
I write this function for ajax
Code: Select all
function ajax(url, callback, error, method, cache, async) {
    async = async || true;
    //alert(cache);
    if (typeof(cache) == 'undefined') {
        cache = false;
    }
    if (typeof(method) == 'undefined') {
        method = 'GET';
    }
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
    {
        xmlhttp = new XMLHttpRequest();
    } else // code for IE5, IE6
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                if (typeof(callback) == 'function') {
                    callback(xmlhttp.responseText);
                }

            } else {

                if (typeof(error) == 'function') {
                    error(xmlhttp.status);
                } else {
                    alert('خطا : لطفا مجددا تلاش کنید.');
                }


            }


        }

    }
    var d = new Date();
    var n = d.getTime();
    var getExplode = url.split("?");
    scriptName = url;
    param = '';
    if (getExplode.length > 1) {
        scriptName = getExplode[0];
        param = getExplode[1];
        if (cache == false) {
            param = param + "&n=" + n;
        }

    } else {
        if (cache == false) {
            param = param + "n=" + n;
        }
    }

    if (method.toLowerCase() == 'post') {
        xmlhttp.open("POST", scriptName, async);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(param);

    } else {
        xmlhttp.open("GET", scriptName + '?' + param, async);
        xmlhttp.send();
    }



}
for example
Code: Select all
     var url = ajaxUrl + "OperationRenovation.php?Command=GetDetail&IdDarkhast=" + ID + "&Code=" + Code + "&Mabna=" + Mabna;
     ajax(url, function(Response) {}, function() {
         alert('مشکل در برقراری ارتباط با سرور');
     }, 'post');

Re: How to Use Ajax OR Retrieve Data from Database IN DynaForm

Posted: Tue Apr 26, 2016 7:29 am
by Kozure
Thanks for sharing.
Code: Select all
ajax(
	url, 
	function(Response) {
	// here you can process the ajax response result.
	}, 
	function() {
         alert('some message ...');
     }, 'post');