duancaiyi7567 2017-09-02 13:05
浏览 61

将JS变量传递给PHP Ajax

Impossible to get out in this passage of variable I tried many things ... It is however a normally not complicated thing ...

I would like to pass a variable: SerialNumberJS which is in the JS (which I retrieve via the URL: getUrlParameter function) to the PHP file! This variable will then be used in the PHP to make a base query. Then go back to the JS to display a graph!

Help please :)

$(document).ready(function(){

    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

    var SerialNumberJS = getUrlParameter('SerialNumber'); 
    console.log(SerialNumberJS);


    $.ajax({
        url : "graphdata.php",
        type : "GET",
        success : function(data){
            console.log(data);

            var heure = [];
            var payload = [];

            for(var i in data) {
                heure.push("Heure " + data[i].heure);
                payload.push(data[i].payload);
            }

            var chartdata = {
                labels: heure,
                datasets: [
                    {
                        label: "payload",
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: "rgba(59, 89, 152, 0.75)",
                        borderColor: "rgba(59, 89, 152, 1)",
                        pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
                        pointHoverBorderColor: "rgba(59, 89, 152, 1)",
                        data: payload
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var LineGraph = new Chart(ctx, {
                type: 'line',
                data: chartdata
            });
        },
        error : function(data) {

        }
    });
});
<?php

//setting header to json
header('Content-Type: application/json');

//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '****');
define('DB_NAME', 'delta');

//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

if(!$mysqli){
    die("Connection failed: " . $mysqli->error);
}

$SerialNumber = "00000000010";

//query to get data from the table
$query = 'SELECT heure, payload FROM reception WHERE Serial_number = "'.$SerialNumber.'" ORDER BY date DESC LIMIT 10';

//execute query
$result = $mysqli->query($query);

//loop through the returned data
$data = array();
foreach ($result as $row) {
    $data[] = $row;
}
//free memory associated with result
$result->close();

//close connection
$mysqli->close();

//now print the data
print json_encode($data);
?>
  • 写回答

1条回答 默认 最新

  • dr637349 2017-09-02 13:30
    关注

    To get the serial number in PHP file you need to pass it in ajax request's data element:

    $.ajax({
            url : "graphdata.php",
            type : "GET",
            data: {
                'SerialNumberJS' : SerialNumberJS // add other if needed
            },
            success : function(data){
                // your code....
            }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?