简体   繁体   中英

Sending Data VIA AJAX and getting JSON Response

I am trying to send data via AJAX and process them in another page with query and get the response to be processed in datatables.

here is my code,

OutstandingProcess.php

    var subjob = '<?php echo $subjob; ?>';
        $.ajax({
            dataType: 'JSON',
            type:"POST",
            data:{subjob:subjob},
            url:'divpages/OutstandingProcessFabJSON.php',
            success : function (data) { 
                alert(data.msg);
            }
        });

and on the OutstandingProcessFabJSON.php,

$subjob = $_POST['subjob'];

$fabDtlSql = oci_parse($conn, "SELECT VFI.* FROM VW_FAB_INFO VFI WHERE VFI.PROJECT_NAME = '$subjob'");
oci_execute($fabDtlSql);

$rows = array();
while ($r = oci_fetch_assoc($fabDtlSql)) {
    $rows[] = $r;
}
$fabDtl = json_encode($rows, JSON_PRETTY_PRINT);
$fabDtlCount = count($rows);

I need to get the response for $fabDtlCount and $fabDtl $fabDtl needed for DataTables ajax call.

So far I get no response. Please help me

You have to print or echo your data in OutstandingProcessFabJSON.php file.

$subjob = $_POST['subjob'];

$fabDtlSql = oci_parse($conn, "SELECT VFI.* FROM VW_FAB_INFO VFI WHERE VFI.PROJECT_NAME = '$subjob'");
oci_execute($fabDtlSql);

$rows = array();
while ($r = oci_fetch_assoc($fabDtlSql)) {
    $rows[] = $r;
}
$fabDtl = json_encode($rows, JSON_PRETTY_PRINT);
$fabDtlCount = count($rows);
echo $fabDtlCount;// this you can capture in ajax success().

Now you want more that one value from ajax filr. So add all required values in to a array, then json_encode() that array

$fabDtl = $rows;// remove encode here
$fabDtlCount = count($rows);
$arr["fabDtl"] = $fabDtl;
$arr["fabDtlCount"] = $fabDtlCount;
echo json_encode($arr);

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