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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图