dtx3006 2015-10-27 13:48
浏览 90

使用AJAX将数据发布到PHP并在PHP中读取结果(使用帖子)

I want to show data recieved from MySQL in Highchart.js graphic. For that, I'm using two textboxes which being entered to date informations. Finally, using a single button which calls function (chart.php). By the way, I'm getting date informations and sending to mysql.php with $.post method in the function. Then I want to get MySQL datas to show in Highchart.js. But program is giving me empty array. Actually, array have a lot of datas, because function(data) is showing datas in the ($.post).

chart.php

function zaman_al()
    {

        var baslangic=document.getElementById('rest_example_3').value;            
        var son=document.getElementById('rest_example_4').value;

        if (baslangic=="" || son=="") 
            {
                alert("Lütfen Tarih bilgisi Giriniz !!!");
            }
        else
        {

            baslangic = new Date(baslangic); 
            baslangic = baslangic.getTime(); 

            son = new Date(son); 
            son= son.getTime(); 


            if (son<=baslangic) 
                {
                    alert("Geçersiz Tarih Girişi !!!");

                }

            if (son>baslangic)
            {

                var veri={'baslama':(baslangic/1000),'bitirme':(son/1000)};
                $.post('mysql.php',veri,
                    function(data)
                    {

                        alert(data);

                        $(function () {
                            $('#kku').highcharts({
                                chart: {
                                    type: 'spline'
                                },
                                title: {
                                    text: 'Snow depth at Vikjafjellet, Norway'
                                },
                                subtitle: {
                                    text: 'Irregular time data in Highcharts JS'
                                },
                                xAxis: {
                                    type: 'datetime',
                                    dateTimeLabelFormats: { // don't display the dummy year
                                        month: '%e. %b',
                                        year: '%b'
                                    },
                                    title: {
                                        text: 'Date'
                                    }
                                },
                                yAxis: {
                                    title: {
                                        text: 'Snow depth (m)'
                                    },
                                    min: 0
                                },
                                tooltip: {
                                    headerFormat: '<b>{series.name}</b><br>',
                                    pointFormat: '{point.x:%e. %b}: {point.y:.2f} m'
                                },

                                plotOptions: {
                                    spline: {
                                        marker: {
                                            enabled: true
                                        }
                                    }
                                },

                                series: [{
                                    name: 'Winter 2007-2008',
                                    // Define the data points. All series have a dummy year
                                    // of 1970/71 in order to be compared on the same x axis. Note
                                    // that in JavaScript, months start at 0 for January, 1 for February etc.
                                    data: [


                                        <?php   

                                                $veri=explode('"',$sonuc);

                                                for ($i = 0; $i < count($veri); $i++) 
                                                {
                                                    if(($i%4)==1)
                                                    {
                                                        echo "[".$veri[$i].","; // time
                                                    }

                                                    if(($i%4)==3)
                                                    {
                                                        echo $veri[$i]."],
"; // sensor_value
                                                    }
                                                };

                                        ?>

                                    ]
                                }]

                            });
                        });
                    }); 
            }  
        }
    }

Above code for button function. By the way.

chart.php

                            <b>Başlangıç Zamanı :</b>&emsp;</td>
                            <td><input style="border-radius:15px" type="textbox" name="rest_example_3" id="rest_example_3" value="" /></td>
                            <td ><br><br><br></td>

                    </tr>

                    <tr>

                            <td><b>Bitiş Zamanı :</b>&emsp;</td>
                            <td><input style="border-radius:15px" type="textbox" name="rest_example_4" id="rest_example_4" value="" /></td>
                            <td ><br><br><br></td>
                    </tr>

                    <tr>
                        <td ><br><br><br></td>
                        <td><input type="button" style="border-radius:15px; background-color:lightgreen; float:right" width="100" value="Get Chart" onClick="zaman_al()"> </td>  
                        <td ><br><br><br></td>
                    </tr>

                </table></center>  

                <div id="kku"></div>     

When I sent to data mysql.php in the ($.POST), mysql.php is giving me notice:Undefined xxx.

mysql.php error statement

By the way, mysql.php is follow as:

<?php

$baslangic =$_POST['baslama'];  // -> notice: Undefined index: baslama
$son=$_POST['bitirme'];         // -> notice: Undefined index: bitirme

//$baslangic=1445780257;    ->    correct result for it
//$son=1445780290;          ->    correct result for it

//echo $son;
//echo $baslangic;
//settype($son, "int");
//settype($baslangic, "int");
//echo gettype(1445780290);
//echo gettype($son);

$baglanti= new mysqli("localhost","root","","deneme_kou");  

$sql = "SELECT zaman,sensor_deger FROM sensor WHERE zaman>='$baslangic' AND zaman<='$son'";
$res=mysqli_query($baglanti,$sql);

$result = array();

while( $row = mysqli_fetch_array($res))
{
    $result[]=array($row["zaman"],$row["sensor_deger"]);
}

$sonuc=json_encode(array($result));

echo json_encode(array($result));

?>

When I wrote $baslangic=1445780257 $son=1445780290; instead of $baslangic = $_POST['baslama']; $son=$_POST['bitirme'], I can get correct result and show in highchart.js.

Could you help me about this problem?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)