0

So, I have a gridbox. If user hits enter, gridbox create a row and load datas inserted by user. Program should run a mysql query with those datas what user insert when he hits enter.

Now I have a html page with a javascriptthat controls the gridbox actions. This js is called when user hits enter:

<script type="text/javascript" >
    var dbs=0;
    var val=[];
    function myFunction(e) {
            if(e.keyCode == 13){
                var newId = (new Date()).valueOf();
                var value=gridbox.cells(1,0).getValue();
                if(value.substring(0,5)=="JrLbl")
                {
//the getfn(value) should returns the value of the mysql query 

                    gridbox.addRow(newId,[value.substring(5,(value.indexOf(".")-4)),getfn(value)]);
                }
                val.push(value);
                gridbox.cells(1,0).setValue('');
                gridbox.editCell(1,0);
                dbs=dbs+1;
            }
    }
    function upload(){
        var jsonString = JSON.stringify(val);

            var jsonData = $.ajax({
                type: "POST",
                  url: "upload.php",
                  data: { 'data' : jsonString},
                  dataType:"json", 
                      async: false
                  }).responseText;


        }
    function getfn(str){
        xmlhttp = new XMLHttpRequest();
        var lblid= str.substring(str.indexOf(".")+6,str.length);
        alert(lblid);
        xmlhttp.open("GET","getfn.php?q="+lblid,true);
        xmlhttp.send();

    }

</script>

And in the getfn.php runs the mysql query:

<?php
header('Content-Type: text/html; charset=UTF-8');
require_once($_SERVER['DOCUMENT_ROOT']."globalfunctions/globalClasses.php");
require_once($_SERVER['DOCUMENT_ROOT'].'globalfunctions/GlobalParameters.php');
error_reporting ( E_ALL ^ E_DEPRECATED);
$q = intval($_GET['q']);

$db_jira = new indotekDbConnection("jira");
            $sql=mysql_query("SELECT FajlNev
                from indoteklabels
                where ID='$q'");
            $FajlNev=mysql_fetch_row($sql);
            return $FajlNev[0];
            $db_jira->Close();
            unset($db_jira);
?>

And it returns the data what should go the marked place

enter image description here

Daniel
  • 33
  • 1
  • 8
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – eisbehr Aug 30 '16 at 08:54
  • you can print echo statement with required output in php then return it from your function like xmlhttp.responseText – Jekin Kalariya Aug 30 '16 at 08:56
  • @JekinKalariya yes I1ve tried, and it doesnt work – Daniel Aug 30 '16 at 08:59
  • @eisbehr Can you explain me why my problem is the same with you mentioned? – Daniel Aug 30 '16 at 09:08

3 Answers3

1

Instead of using return $FajlNev[0]; use $data = $FajlNev[0]; echo json_encode($data);

Kushagara.K
  • 67
  • 2
  • 8
0
function getfn(str){
        xmlhttp = new XMLHttpRequest();
        var lblid= str.substring(str.indexOf(".")+6,str.length);
        //alert(lblid);
        xmlhttp.open("GET","getfn.php?q="+lblid,true);

        xmlhttp.responseType = 'text';

        xmlhttp.onload = function () {
            if (xmlhttp.readyState === xmlhttp.DONE) {
                if (xmlhttp.status === 200) {
                    var newId = (new Date()).valueOf();
                    gridbox.addRow(newId,[str.substring(5,(str.indexOf(".")-4)),xmlhttp.response]);
                    gridbox.cells(1,0).setValue('');
                    gridbox.editCell(1,0);
                    //console.log(xmlhttp.response);
                    //console.log(xmlhttp.responseText);
                }
            }
        };
        xmlhttp.send();


    }
Daniel
  • 33
  • 1
  • 8
0

Your PHP scripts is generating a blank page. The return keyword has no meaning outside of a function, it just terminates the script. If you want to process the results you need to explicitly echo the values onto the page.

Navigate to getfn.php?q=123 in your web browser. Whatever you see there is exactly what your AJAX request is going to see. Right now it's going be a blank page. If you want JSON then do echo json_encode($the_data_i_want_back)

Aside: You seem to be using jQuery. If so you're probably best using jQuery's built in, cross browser AJAX functionality rather than using raw JS XMLHttpRequest stuff.

ReallyMadeMeThink
  • 1,061
  • 7
  • 11