简体   繁体   English

如何检索jquery ajax对php变量的响应?

[英]How to retrieve jquery ajax response to php variable?

How to pass an jquery ajax response after success into a php variable. 如何在成功之后将jquery ajax响应传递给php变量。 this is my code : 这是我的代码:

process.php 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 的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. 当我将响应放入div($('#content')。append(resp);)时,没有问题,但是如何将响应放入php变量。 Thanks for advance.. 谢谢你提前..

update my Highcharts code : 更新我的Highcharts代码:

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. 我将highcharts配置为函数。 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.. 当我发出警报(datan)时,它显示了来自ajax响应的数据,但是当我输入Drilldown选项数据时(参见上面的符号#*),高图配置无法读取它。

PHP runs on server not on client so the thing you asking is possible only on server. PHP在服务器上而不是在客户端上运行,所以你要求的东西只能在服务器上运行。 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);



}

Javascript is a client side scripting. Javascript是客户端脚本。 You can't assign the javascript value directly to the php variable. 您不能将javascript值直接分配给php变量。

  1. One way is to set a SESSION variable in PHP. 一种方法是在PHP中设置SESSION变量。 In your case in process.php file. 在您的情况下在process.php文件中。
  2. Another way is to use Database/files. 另一种方法是使用数据库/文件。

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. 好吧,如果你真的想这样做,你可以在Javascript中设置一个带有所需变量的cookie,然后用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. 你可以在客户端做任何你想做的事情,当你想将变量传输到服务器端时,只需在php页面上打开一个ajax连接,它将处理变量并通过POST数据将实际变量传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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