1

How to pass an jquery ajax response after success into a php variable. this is my code :

process.php

    $start = "";
    $end = "";
    if(isset($_POST['tampStart']))
    {
        $start = $_POST['tampStart'];
    }
    if(isset($_POST['tampEnd']))
    {
        $end = $_POST['tampEnd'];
    }

    $SQL = "SELECT * FROM `values` WHERE date BETWEEN '".$start."' and '".$end."'";
    $result = mysql_query($SQL);

    $prefix = '';
    while ( $row = mysql_fetch_assoc( $result ) ) {
        $prefix .= "[\n"."'".$row['month']."'".', '.$row['days']."]".",";
    }

    echo rtrim($prefix, ",");

index.php

    var dStart = $('#textInput1').val();
var dEnd = $('#textInput2').val();

var form_data = {
    tampStart: dStart,
    tampEnd: dEnd
};

$.ajax({
    url: 'process.php',
    type: 'POST',
    async : true, 
    data: form_data,
    dataType: 'text',
    success: function(resp){                            
        $('#content').html(resp); 
        //pass to php variable ?
    }
});

there is no problem when I put the response into a div ($('#content').append(resp);), but how to put the response into an php variable. Thanks for advance..

update my Highcharts code :

function createChart(datan) {
                 //alert(datan);       
                    Highcharts.setOptions({
                        lang: {
                            drillUpText: 'Back to {series.name}'
                        }
                    });

                    var options = {

                        chart: {
                            height: 300
                        },

                        title: {
                            text: 'Highcharts Drilldown Plugin'
                        },

                        xAxis: {
                            categories: true
                        },

                        drilldown: {
                            series: [{
                                id: 'fruits',
                                name: 'Fruits',
                                data: [datan]  //here #*
                            }, {
                                id: 'cars',
                                name: 'Cars',
                                data: [{
                                    name: 'Toyota', 
                                    y: 4,
                                    drilldown: 'toyota'
                                },
                                ['Volkswagen', 3],
                                ['Opel', 5]
                                ]
                            }, {
                                id: 'toyota',
                                name: 'Toyota',
                                data: [
                                    ['RAV4', 3],
                                    ['Corolla', 1],
                                    ['Carina', 4],
                                    ['Land Cruiser', 5]
                                ]
                            }]
                        },

                        legend: {
                            enabled: false
                        },

                        plotOptions: {
                            series: {
                                dataLabels: {
                                    enabled: true
                                },
                                shadow: false
                            },
                            pie: {
                                size: '80%'
                            }
                        },

                        series: [{
                            name: 'Overview',
                            colorByPoint: true,
                            data: [{
                                name: 'Fruits',
                                y: 10,
                                drilldown: 'fruits'
                            }, {
                                name: 'Cars',
                                y: 12,
                                drilldown: 'cars'
                            }, {
                                name: 'Countries',
                                y: 8
                            }]
                        }]
                    };

                    // Column chart
                    options.chart.renderTo = 'container1';
                    options.chart.type = 'column';
                    var chart1 = new Highcharts.Chart(options);

                }

I make the highcharts config to a function. when I alert(datan), its shown the data from ajax response, but when I put on Drilldown option data (see sign #* above), the highchart config cant read it..

20
  • Can you explain what do you want to do after getting the response.?Whats is the need of saving the response to a php variable.? Commented Dec 10, 2013 at 6:04
  • 1
    @Deepu The OP want to get the AJAX response in JQuery and assign into a PHP variable Commented Dec 10, 2013 at 6:06
  • I want to use that php variable to set data option of Highchart, the result will be ['august', 30], ['september', 30],... etc. but now I only can put that response into div, cant to php variable.. Commented Dec 10, 2013 at 6:09
  • 1
    Yes, It is impossible, because PHP scripts are running on the server side. AJAX is a client side technology - Asynchronous JavaScript And XML Commented Dec 10, 2013 at 6:09
  • 1
    success: function(data){ options.series[0].setData(data); } Commented Dec 10, 2013 at 6:17

3 Answers 3

2

PHP runs on server not on client so the thing you asking is possible only on server. You have to customize this code for your need it gives framework.

$.ajax({
    url: 'process.php',
    type: 'POST',
    async : true, 
    data: form_data,
    dataType: 'text',
    success: function(resp){                            
       // $('#content').html(resp); 
        createChart(resp);
    }
});
var chart;
function createChart(data) {
var options = {

    chart: {
        height: 300
    },

    title: {
        text: 'Highcharts Drilldown Plugin'
    },

    xAxis: {
        categories: true
    },

    drilldown: {
        series: data}
};


options.chart.renderTo = 'content';
options.chart.type = 'column';
var chart1 = new Highcharts.Chart(options);



}
Sign up to request clarification or add additional context in comments.

1 Comment

I think This is not an answer to the question
1

Javascript is a client side scripting. You can't assign the javascript value directly to the php variable.

  1. One way is to set a SESSION variable in PHP. In your case in process.php file.
  2. Another way is to use Database/files.

Comments

0

Well, if you really want to do it this way, you can set a cookie with the desired variable in Javascript and afterwards access it with PHP.

But, there is another way. You can do whatever you want on the client side and when you want to transmit the variable to server-side, just open an ajax connection on the php page which will handle the variable and hand it the actual variable through POST data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.