dton37910 2018-02-15 15:02
浏览 58
已采纳

Ajax调用。 将值传递给另一个php文件

I have a problem and hope you can help. Ii have a status.PHP file containing a js.

STATUS.PHP

<? ..stuff... ?>
<html>
    <head>
        <title>BCM Status Page</title>
        <script src="jquery-3.3.1.min.js"></script>
        <script src="updater.js"></script>
    </head>
    <body bgcolor="#305c57" onload='init();'>

As you can see in the html ihave included a JS, during "onload" i'm calling the init() function of the javascript called updater.js

Now in the UPDATER.JS

function init() {
    setInterval(read, 2000)
}

function read() {
   $.ajax({
        type: 'POST',
        url: 'readDB.php',
        dataType: 'jsonp',
        success: function (data) {
            console.log(data);
            var json_obj = $.parseJSON(data);
            console.log(json_obj[0].gwnumber);   
        },
        error: function () {
            console.log("Error loading data");
        }
    });

}

I'm doing an ajax call to the readDB.php that is working as intended, infact i have the correct value in the json_obj.

My question is: how can i get the json_obj value and pass it to the status.PHP file that is the one who's including the JS too? Hope you can help. TY

  • 写回答

3条回答 默认 最新

  • dqbn76906 2018-02-15 15:32
    关注

    Ok, there is a lot to say in this argument, but i will be the briefiest possible.

    first things first

    php and Javascript are two different programming language with a completely different paradigm. The first is a back-end focused programming language;

    Javascript instead is more front-end focused, just for entirety i have to mention that JS is used also for the backend part with a special eviroment called Node.js

    back to the problem, the things that you are trying to do is not impossible but is excactly as you asked, your're idea (if i got it) was to pass the data from the js to the php like a parameter in a function...

    the thing is that the php is elaborate and renderizated before in the server and the javascript is executed in the client, in the client web page there is no more footprint the php. This process is described very well at this link: http://php.net/manual/en/intro-whatis.php

    The possible solution is:

    FRONT-END(js): make another ajax call(request) to the same page that you are displaying with all the data that you want to elaborate.

    BACK-END(php): controll if this request has been made, then access the data with the global variables $_POST & $_GET (depending on the type of the request), then elaborate this data.

    if I can I suggest you to make a check if the manipulation that you want to do on those data need to be done in the server-side and not by the js!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog