weixin_33736048 2015-04-22 08:29 采纳率: 0%
浏览 15

用ajax获取变量

I have a ajax script to a folderwatcher. The folderwatcher will return $dir. The page with the ajax script will later on get the dir and then echo it out later in the php code.

How to i get the variable so i can just echo it out?

Ajax page:

<!DOCTYPE html>
<head>

<script type="text/javascript"> 
    $(document).ready(function(){
        setInterval(function(){ 
        //code goes here that will be run every 5 seconds.    
        $.ajax({
            type: "POST",
            url: "checkfolder.php",
            success: function(result) {
                //something that will get the varaible?
                }
            });
        }, 5000);
    });

    </script>

</head>
<body>
    <?php
        echo $dir;
    ?>
</body>
</html>

The folder watcher, that returns the right dir:

    <?php
// Configuration ///////////////////////////////////////////////////////////////
$host ='ftp.dodododo.com';
$port = 21;
$user = 'xoxo';
$pass = 'xoxo';
$cache_file = 'ftp_cache';

$dir = "../img/uploads/viktigt/";
$filesfound = false;

//kollar upp mappen viktigt
if (!is_dir_empty($dir)) {
    $filesfound = true;
    $filetype = "img";
                    }

//kollar igenom om det finns filer i standrard
if ($filesfound == false){
    $dir = "../img/uploads/standard/";
    if (!is_dir_empty($dir)) {
        $filesfound = true;
        $filetype = "img";
    }
 }

//Funktion som kollar mapparna
                    function is_dir_empty($dir) {
                      if (!is_readable($dir)) return NULL; 
                      $handle = opendir($dir);
                      while (false !== ($entry = readdir($handle))) {
                        if ($entry != "." && $entry != "..") {
                          return FALSE;
                        }
                      }
                      return TRUE;
                    }


// Main Run Program ////////////////////////////////////////////////////////////

// Connect to FTP Host
$conn = ftp_connect($host, $port) or die("Could not connect to {$host}
");

// Login
if(ftp_login($conn, $user, $pass)) {
ftp_pasv($conn,true);   // Ny rad lägg in den
 // Retrieve File List
  $files = ftp_nlist($conn, $dir);

  // Filter out . and .. listings
  $ftpFiles = array();
  foreach($files as $file)
  {
    $thisFile = basename($file);
    if($thisFile != '.' && $thisFile != '..') {
      $ftpFiles[] = $thisFile;
    }
  }

  // Retrieve the current listing from the cache file
  $currentFiles = array();
  if(file_exists($cache_file))
  {
    // Read contents of file
    $handle = fopen($cache_file, "r");
    if($handle)
    {
      $contents = fread($handle, filesize($cache_file));
      fclose($handle);

      // Unserialize the contents
      $currentFiles = unserialize($contents);
    }
  }

  // Sort arrays before comparison
  sort($currentFiles, SORT_STRING);
  sort($ftpFiles, SORT_STRING);

  // Perform an array diff to see if there are changes
  $diff = array_diff($ftpFiles, $currentFiles);
  if(count($diff) > 0)
  {
    echo "1";//New file/deleted file
  }
  else{
   $diff = array_diff($currentFiles,$ftpFiles);
      if(count($diff) > 0)
      {
       echo $dir;
      }
  }

  // Write new file list out to cache
  $handle = fopen($cache_file, "w");
  fwrite($handle, serialize($ftpFiles));
  fflush($handle);
  fclose($handle);
}
else {
  echo "Could not login to {$host}
";
}

// Close Connection
ftp_close($conn);
?>
  • 写回答

1条回答 默认 最新

  • weixin_33713350 2015-04-22 08:34
    关注

    How to i get the variable so i can just echo it out?

    You have to "echo" your html (or json) structure in the php script.

    Then with jQuery paste it in the main html:

    success: function(result) {
        $('body').html(result);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)