Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax表单将变量返回到php_Php_Ajax_Forms - Fatal编程技术网

Ajax表单将变量返回到php

Ajax表单将变量返回到php,php,ajax,forms,Php,Ajax,Forms,我还有一个问题: Ajax表单运行良好。他们中的大多数人都需要做mysql的工作,并且只有在条目是否可以写入时才返回值。我用的只是附和陈述。例如,回声“1”;如果可以写入值并回显“2”;如果无法写入值 现在我需要调用3个变量。我知道我可以把它们写成数组。我的问题是,我不能将这个变量返回到我的可见站点 这是我的JavaScript代码: //Show statistic $('.statistic_submit').click(function(){ if ($('#month').val

我还有一个问题:

Ajax表单运行良好。他们中的大多数人都需要做mysql的工作,并且只有在条目是否可以写入时才返回值。我用的只是附和陈述。例如,回声“1”;如果可以写入值并回显“2”;如果无法写入值

现在我需要调用3个变量。我知道我可以把它们写成数组。我的问题是,我不能将这个变量返回到我的可见站点

这是我的JavaScript代码:

//Show statistic
$('.statistic_submit').click(function(){
    if ($('#month').val() == 'none' || $('#year').val() == 'none') {
        $("#dialog_empty").dialog( "open" );
        return false;
    }

    var form = $('#statistic_view');  
    var data = form.serialize(); 

    $.ajax({
        url: "include/scripts/user_statistic.php",
        type: "POST",
        data: data,
        success: function (reqCode) {
            if (reqCode == 1) {
                //Show generated table
                $('.done').fadeOut('slow'); 
                $('.done').fadeIn('slow');
            }
            if (reqCode == 2) {
                //No values found
                $('.done').fadeOut('slow');
                $("#dialog_error").dialog( "open" );
            }
        }
    });
    return false;
});
这是我的html代码:

<div>
    <form id="statistic_view" action="include/scripts/user_statistic.php" method="post">
        <select name="month" id="month">
            <option value="none" class="bold italic">Monat</option>
            <?php 
                for($i=1; $i<=12; $i++){
                    if($i == $month)
                        echo "<option value=\"".$i."\" selected>".$month_name[$i]."</option>\n";
                    else
                        echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
                }
            ?>
        </select>
        <select name="year" id="year">
            <option value="none" class="bold italic">Jahr</option>
            <?php 
                for($i=2012; $i<=$year; $i++){
                    if($i == $year)
                         echo "<option value=\"".$i."\" selected>".$i."</option>\n";
                    else
                        echo "<option value=\"".$i."\">".$i."</option>\n";
                }
            ?>
        </select>
        <br/><br/>
        <div id="user_statistic">
            <input type="submit" id="small" class="statistic_submit" value="Daten anzeigen">
        </div>
    </form> 
    <br />
    <div class="done">
        <p class="bold center"><?php echo "Besucher ".$month_name[$month]." ".$year; ?></p>
        <canvas id="cvs" width="680" height="250">[No canvas support]</canvas>
        <script>
            chart = new RGraph.Line('cvs', <?php print($data_string) ?>);
            chart.Set('chart.tooltips', <?php print($labels_tooltip) ?>);
            chart.Set('chart.tooltips.effect', 'expand');
            chart.Set('chart.background.grid.autofit', true);
            chart.Set('chart.gutter.left', 35);
            chart.Set('chart.gutter.right', 5);
            chart.Set('chart.hmargin', 10);
            chart.Set('chart.tickmarks', 'circle');
            chart.Set('chart.labels', <?php print($labels_string) ?>);        
            chart.Draw();
        </script>
    </div>
</div>

莫纳特
加尔



[无画布支持] chart=新的RGraph.Line('cvs',); chart.Set('chart.tooltips',); Set('chart.tooltips.effect','expand'); Set('chart.background.grid.autofit',true); 图表集('chart.Gottle.left',35); chart.Set('chart.ground.right',5); 图表集('chart.hmargin',10); chart.Set('chart.tickmarks','circle'); chart.Set('chart.labels',); chart.Draw();
这是我的用户_statistic.php:

... (mysql stuff)
    /******************************/
    /** Create diagram
    /******************************/
    $labels = array();
    $data   = array();

    for ($j=1; $j<=$days; $j++) {
        $labels[$j] =$j;
        $data[$j] = $day_value[$j];
    }

    // Aggregate all the data into one string
    $data_string = "[" . join(", ", $data) . "]";
    $labels_string = "['" . join("', '", $labels) . "']";
    $labels_tooltip =  "['" . join("', '", $data) . "']";

    //data written 
    echo "1";
。。。(mysql资料)
/******************************/
/**创建图表
/******************************/
$labels=array();
$data=array();

对于($j=1;$j避免自己将数组转换为字符串。如果需要将PHP数组传递回jQuery,则应使用以下函数:

echo json_encode( $array );
这将作为一个JSON对象,然后您可以处理客户端。您的JSON字符串将返回到
$的回调中。ajax
方法:

$.ajax({
  url: "include/scripts/user_statistic.php",
  type: "POST",
  data: data,
  dataType: 'json',
  success: function ( response ) {
     /* response is your array, in JSON form */
  }
});
例如,如果我们的PHP脚本执行以下操作:

$response = array(
  'message' => 'Success',
  'allData' => array( 'Jonathan', 'Mariah', 'Samuel', 'Sally' )
);

echo json_encode( $response );
我们可以从jQuery中警告
消息
,如下所示:

success: function ( response ) {
   alert( response.message );
}

这里最好的方法是返回一个json对象-

$response['error_code'] = '1'; //everything ok. 0 if not ok
$response['data_string'] = 'this will have some data';
$response['labels_string'] = 'labels';
$response['labels_tooltip' = 'here goes the tooltips';
echo json_encode($response);
在javascript代码中,将返回数据类型称为json-

$.ajax({
    url: "include/scripts/user_statistic.php",
    type: "POST",
    data: data,
    dataType: json,
    success: function (reqCode) {
        if (reqCode.error_code == 1) {
            alert('this is the data string '+resCode.data_string);
            //Show generated table
            $('.done').fadeOut('slow'); 
            $('.done').fadeIn('slow');
        }
        if (reqCode.error_code == 2) {
            //No values found
            $('.done').fadeOut('slow');
            $("#dialog_error").dialog( "open" );
        }
    }
});

不要忘记将
数据类型
指定为
json
。或者在服务器端设置头,指定响应为json。以及如何在php中处理json响应?因为必须将变量写入.done容器以显示图表。@JPM您不在php.php中处理json响应向JavaScript发送JSON响应。JavaScript处理JSON数据。我在上面的回答底部向您展示了如何访问JSON对象的成员。啊,好的,实际上我可以将脚本(.done container)部分写入(reqCode==1)部分。但如何将其放入容器中?因为它必须显示在我的html文件中的正确位置。@JPM您可以像分配其他内容一样将JSON响应的部分分配给元素:
$(.done”).html(response.message);