dton37910
2018-02-15 15:02
浏览 56

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

图片转代码服务由CSDN问答提供 功能建议

我遇到了问题,希望你能提供帮助。 我有一个包含js的status.PHP文件。

<强> STATUS.PHP

 <代码>&LT ;?  ..stuff ...?&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; title&gt; BCM状态页&lt; / title&gt; 
&lt; script src =“jquery-3.3.1.min.js  “&gt;&lt; / script&gt; 
&lt; script src =”updater.js“&gt;&lt; / script&gt; 
&lt; / head&gt; 
&lt; body bgcolor =”#305c57“onload ='init(  );'&gt; 
   
 
 

正如你在html中看到的ihave中包含一个JS,在“onload”期间我正在调用javascript的init()函数 名为updater.js

现在位于 UPDATER.JS

 函数init(){
 setInterval  (阅读,2000)
} 
 
函数read(){
 $ .ajax({
 type:'POST',
 url:'readDB.php',
 dataType:'jsonp',\  n success:function(data){
 console.log(data); 
 var json_obj = $ .parseJSON(data); 
 console.log(json_obj [0] .gwnumber); 
},
错误 :function(){
 console.log(“加载数据错误”); 
} 
}); 
 
} 
   
 
 

我' 我正在做ajax cal l对于正在按预期工作的readDB.php,事实上我在json_obj中有正确的值。

我的问题是:如何获取json_obj值并将其传递给状态 .PHP文件也是包含JS的人? 你可以帮忙。 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!

    已采纳该答案
    打赏 评论
  • dsfsw1233 2018-02-15 15:10

    Consider the order of execution:

    1. User visits status.php
    2. Browser requests status.php
    3. Server executes status.php and sends response to browser
    4. JS requests readDB.php
    5. Browser requests readDB.php
    6. Server executes readDB.php and sends response to browser
    7. JS processes response
    8. Go To 4

    By the time you get to 7, it is too late to influence what happens at step 2.

    You could make a new Ajax request to status.php and process the response in JS, but since status.php returns an entire HTML document, that doesn't make sense.

    You could use location to load a new page using a URL that includes status.php and a query string with information from the Ajax response, but that would making using Ajax in the first place pointless.

    You should probably change readDB.php to return *all** the data you need, and then using DOM methods (or jQuery wrappers around them) to modify the page the user is already looking at.

    打赏 评论
  • donglang8008 2018-02-15 17:07

    The simpliest and fastest (maybe not the sexiest way) to do it :

    • create global variable var respondData; in STATUS.PHP
    • within you ajax request on success function assign your data callback to it

    respondData = data;

    Now you have an access to it from every place in your code even when the ajax request is done. Just bare in mind to ensure you will try to access this variable after the page will fully load and after ajax will process the request. Otherwise you will get 'undefined'

    打赏 评论

相关推荐 更多相似问题