简体   繁体   中英

How to return values from php called by ajax?

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 javascript that 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

在此处输入图片说明

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

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();


    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM