Jump to content

Get AJAX response from two PHP array


Senthilkumar

Recommended Posts

Dear Team,

I am having a table which is displaying actual and target values from two different mysql table. 

My table is image.thumb.png.75f2c55963fa2d53ab3dab4cb74e6115.png

In above table when i am selecting the month the actual and target date should display from the databse. 

When i am using the bellow code all the target value is displaying

	<script>//..................month hide function
        function myFunction() {


               var month = document.getElementById("month").value;
               var dealerid = document.getElementById("dealerid").value;

		        $.ajax({
                    type: 'POST',
                    dataType: 'JSON',
                   url: 'Revenue_Data.php',
                  data: 'month=' + month + '&dealerid=' + dealerid,
		          //data: 'month=' + month + '&dealerid=' + dealerid,
                    success: function (response) {

                        //var jsonData = JSON.parse(response);

                        var len = response.length;
                        //alert(jsonData);

                        for (var i = 0; i < len; i++) {
                                var Category_Name = response[i].Category_Name;
                                var Description = response[i].Description;
                                var Month_Value = response[i].Month_Value;
                                        var result = response[i].result;
                                        //var Excavator = response[i].Excavator;
                                        //var Grader = response[i].Grader;
                                        //var WheelLoader = response[i].WheelLoader;
                                        //var SLM = response[i].SLM;
                                        //var Parts_Service_ID = response[i].Parts_Service_ID;
                                        //var Revanue = response[i].Revanue;
                                        //var Margin = response[i].Margin;
                            
                            document.getElementById(result).value = Month_Value;
                            //document.getElementById('ExcavatorActual'+Parts_Service_ID).value = Excavator;
                            //document.getElementById('GraderActual'+Parts_Service_ID).value = Grader;
                            //document.getElementById('WheelLoaderActual'+Parts_Service_ID).value = WheelLoader;
                            //document.getElementById('SLMActual'+Parts_Service_ID).value = SLM;
                            //document.getElementById('RevenueActual'+Parts_Service_ID).value = Revanue;
                            //document.getElementById('Margin'+Parts_Service_ID).value = Margin;
                        }                          


                    }
               });


           }

	</script>
<?php
// Include the database config file
include("../connection.php");
//error_reporting(0);
error_reporting(E_ALL);

ini_set('display_errors', '1');
//ini_set('display_errors','Off');

if(!empty($_POST["month"]) ){
    $Month=$_POST["month"];
    $dealerid = $_POST["dealerid"];
    $Mon = date('M', strtotime($Month));


		$sql = "select $Mon,Category_Name,Description from revenuetarget WHERE Dealer_ID = '$dealerid'";
		$result = mysqli_query($conn, $sql);

        while ($row = mysqli_fetch_array($result)) {

            $Category_Name = $row['Category_Name'];
            $Description = $row['Description'];
            $Month_Value = $row[$Mon];

            $Description1=  str_replace(' ', '', $Description);
            $Category_Name1= str_replace(' ', '', $Category_Name);

        $result1 = trim($Category_Name1);
        $result1 .= trim($Description1);

        $return_arr[] = array(
                "Category_Name" => $Category_Name,
                "Description" => $Description,
                "result" => $result1,
                "Month_Value" => $Month_Value,
            );

        }

        $sql = "select * from revenue_data WHERE dealerid = '$dealerid' AND month = '$Month'";
		$result = mysqli_query($conn, $sql);

        while ($row = mysqli_fetch_array($result)) {

            $Excavator = $row['Excavator'];
            $Grader= $row['Grader'];
            $WheelLoader = $row['WheelLoader'];
            $SLM = $row['SLM'];
            $Parts_Service_ID = $row['Parts_Service_ID'];
            $Revanue = $row['Revanue'];
            $Margin = $row['Margin'];


 

            $return_arr1[] = array(
                    "Excavator" => $Excavator,
                    "Grader" => $Grader,
                    "WheelLoader" => $WheelLoader,
                    "SLM" => $SLM,
                    "Parts_Service_ID" => $Parts_Service_ID,
                    "Revanue" => $Revanue,
                    "Margin" => $Margin,
                );

        }


    echo json_encode($return_arr);
    //echo json_encode($return_arr1);
}
?>

image.thumb.png.188d27b0beec4817bb2a582eec34b7b1.png

 

When i am using the bellow code all my actual values are displaying

	<script>//..................month hide function
        function myFunction() {


               var month = document.getElementById("month").value;
               var dealerid = document.getElementById("dealerid").value;

		        $.ajax({
                    type: 'POST',
                    dataType: 'JSON',
                   url: 'Revenue_Data.php',
                  data: 'month=' + month + '&dealerid=' + dealerid,
		          //data: 'month=' + month + '&dealerid=' + dealerid,
                    success: function (response) {

                        //var jsonData = JSON.parse(response);

                        var len = response.length;
                        //alert(jsonData);

                        for (var i = 0; i < len; i++) {
                                //var Category_Name = response[i].Category_Name;
                                //var Description = response[i].Description;
                                //var Month_Value = response[i].Month_Value;
                                        //var result = response[i].result;
                                        var Excavator = response[i].Excavator;
                                        var Grader = response[i].Grader;
                                        var WheelLoader = response[i].WheelLoader;
                                        var SLM = response[i].SLM;
                                        var Parts_Service_ID = response[i].Parts_Service_ID;
                                        var Revanue = response[i].Revanue;
                                        var Margin = response[i].Margin;
                            
                            //document.getElementById(result).value = Month_Value;
                            document.getElementById('ExcavatorActual'+Parts_Service_ID).value = Excavator;
                            document.getElementById('GraderActual'+Parts_Service_ID).value = Grader;
                            document.getElementById('WheelLoaderActual'+Parts_Service_ID).value = WheelLoader;
                            document.getElementById('SLMActual'+Parts_Service_ID).value = SLM;
                            document.getElementById('RevenueActual'+Parts_Service_ID).value = Revanue;
                            document.getElementById('Margin'+Parts_Service_ID).value = Margin;
                        }                          


                    }
               });


           }

	</script>
<?php
// Include the database config file
include("../connection.php");
//error_reporting(0);
error_reporting(E_ALL);

ini_set('display_errors', '1');
//ini_set('display_errors','Off');

if(!empty($_POST["month"]) ){
    $Month=$_POST["month"];
    $dealerid = $_POST["dealerid"];
    $Mon = date('M', strtotime($Month));


		$sql = "select $Mon,Category_Name,Description from revenuetarget WHERE Dealer_ID = '$dealerid'";
		$result = mysqli_query($conn, $sql);

        while ($row = mysqli_fetch_array($result)) {

            $Category_Name = $row['Category_Name'];
            $Description = $row['Description'];
            $Month_Value = $row[$Mon];

            $Description1=  str_replace(' ', '', $Description);
            $Category_Name1= str_replace(' ', '', $Category_Name);

        $result1 = trim($Category_Name1);
        $result1 .= trim($Description1);

        $return_arr[] = array(
                "Category_Name" => $Category_Name,
                "Description" => $Description,
                "result" => $result1,
                "Month_Value" => $Month_Value,
            );

        }

        $sql = "select * from revenue_data WHERE dealerid = '$dealerid' AND month = '$Month'";
		$result = mysqli_query($conn, $sql);

        while ($row = mysqli_fetch_array($result)) {

            $Excavator = $row['Excavator'];
            $Grader= $row['Grader'];
            $WheelLoader = $row['WheelLoader'];
            $SLM = $row['SLM'];
            $Parts_Service_ID = $row['Parts_Service_ID'];
            $Revanue = $row['Revanue'];
            $Margin = $row['Margin'];


 

            $return_arr1[] = array(
                    "Excavator" => $Excavator,
                    "Grader" => $Grader,
                    "WheelLoader" => $WheelLoader,
                    "SLM" => $SLM,
                    "Parts_Service_ID" => $Parts_Service_ID,
                    "Revanue" => $Revanue,
                    "Margin" => $Margin,
                );

        }


    //echo json_encode($return_arr);
    echo json_encode($return_arr1);
}
?>

image.thumb.png.5b39457dd0056ef7745cdf85564cd04c.png

 

I want to display botht the values (Actual and target) on same time. How can i get two array values on same time and display on the table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.