dra87370 2016-08-17 08:15 采纳率: 100%
浏览 50

无法使用jquery / ajax POST变量

I have an XML file that changes every few seconds. I would like to saw the data in a div (with the help of jQuery/ajax). I manage to make data autoreload with the help of simplexml_load_string, but because the reload is happening only in a div, I can't use PHP commands like $_POST to keep and the old data between reloads. I would like to have a very simple example of code that takes a variable between ajax div reloads. I try this so far but it is not working.

First try
html file:

<script src="jQuery_v2.1.1.js" type="text/javascript">test</script>

<script>    
$(document).ready(function(){
    setInterval(function(){
        $("#screen").load('phpcode.php')
    }, 5000);

    $.ajax({
        type: 'POST',
        url: 'test.php',
        data: {'varcount': $varcount}
    });
});
</script>
<div id="screen"></div>

phpcode.php

<div>
<?php
    $varcount = $_POST['varcount'];
    $varcount += 1;
    echo  "Test ".$varcount."<br>";
?>
</div>

Second try (I tryed to convert javascript variable to PHP so that i can POST it, and then convert it again to javascript so that i can sent it back via jquery/ajax).

test.php:

<script src="jQuery_v2.1.1.js" type="text/javascript">test</script>
<script>    
$(document).ready(function(){
    setInterval(function(){
        $("#screen").load('phpcode.php')
    }, 5000);
    $.post("test.php", { varcount: varcount } );        
});
</script>
<div id="screen"></div>

phpcode.php

<div>
<?php
    $varcount = $_POST['varcount'];   
    $varcount += 1;
    echo  "Test ".$varcount."<br>";
?>
<script>
    var varcount = <?php echo "$varcount"; ?>    // convet it to javascript variable
</script>
</div>

Questions are:
1. What is wrong in the above code/s and I can't POST any variable?
2. Because XML file is very big (120kb) and reloads every few seconds might be problem for the server, is there any better way to do it?
3. I will have to POST many different array in my final code. How would be able to POST 2 different arrays?

Thanks in advance. Any help would be appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?