php while loop and jquery ajax


php while loop and jquery ajax

这可能是一件非常简单的事情,我只是让它变得更加困难。我有一个php页面,它只有一个复选框和一个按钮。当我点击按钮时,它应该调用我的"collection.php"页面,然后更新我的索引页面的状态。我在collection.php中删除了大部分代码,但我的索引页仍然没有更新。我做错了什么??提前感谢!

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-gb" xml:lang="en-gb">
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            // Click trigger for image
            $("#myBtn").click(function() {
                if ($('#update').is(":checked"))
                    var update = 'on';
                else
                    var update = 'off';
                $.ajax({
                    url: 'collection.php',
                    type:'POST',
                    dataType: 'json',
                    data: 'update='+update,
                    cache: false,
                    async: false,
                    success: function(output_string){
                            //alert(JSON.stringify(output_string));
                            $('#collection_status').html(''); // Clear #wines div
                            $('#collection_status').append(output_string.worked + '<br/>');
                    }, // End of success function of ajax form
                    error: function() {
                        alert("there is an issue with the collection!");
                    }
                }); // End of ajax call
                return false; // keeps the page from not refreshing 
            });
        });
    </script>
</head>
<body style="background-color:#333333;color:white;font-family:verdana;text-transform:lowercase;">
    <form>
        <input type="checkbox" id="update" /> Update Existing<br />
        <button id="myBtn">Collect</button>
    </form>
<br />
<div id="collection_status"></div>
</body>
</html>

collection.php

<?
// movie search variables
$dir = "/video/Movies/";
$movie_extensions = array('mkv','mp4','m4v','avi');
$update_movies = 0;
if(isset($_POST["update"]))
    $update_movies = $_POST["update"];
// read contents from $dir
if (is_dir($dir)){
    if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
            $file_info = pathinfo($file);
            $output_string['worked'] = 'found <span style="color:yellowgreen;">' . $file . '</span><br />';
            echo json_encode($output_string);
        }
        closedir($dh);
    }
}

当我从收集脚本中删除echo部分时,我收到一个响应(如下所示):

$output_string['worked'] = 'testing';
echo json_encode($output_string);

一旦我把它添加回循环中,它就会出错。

作为一个测试,我在那里添加了一个for循环,它也失败了:

for($i=0;$i<10;$i++)
{
    $output_string['worked'] = 'found <span style="color:yellowgreen;">' . $file . '</span><br />';
    echo json_encode($output_string);
}

也许是附带考虑,但可能在评论之外讨论得更好:

你知道$dir = "/video/Movies/";会打开根目录吗?而不是你的collection.php所在路径的子目录。因此(在普通的网络服务器上),我希望你的脚本甚至没有打开该目录的权限;这样就不会在脚本中继续了。

您的数据属性似乎是错误的。

它应该像;

data: {update:update},

您应该更改数据行。

 dataType: 'json',  
 data: {update:update},
 cache: false,  

中的一个示例http://api.jquery.com/jQuery.ajax/
将一些数据保存到服务器,并在完成后通知用户。

$.ajax({  
  type: "POST",  
  url: "some.php",  
  data: { name: "John", location: "Boston" }  
}).done(function( msg ) {  
  alert( "Data Saved: " + msg );  
});

如果您希望接收JSON,则需要发送有效的JSON。

从客户端的角度来看,在while循环中回声出一系列单独的JSON字符串会产生无效的JSON。

如果您使用循环来构建响应对象,那么您应该只在循环中构建对象/数组,然后在构建对象/阵列后json_encode()整个过程并输出响应。

因此,问题看起来是您有两个条件来包装响应输出,而您没有正确解释该故障:

// read contents from $dir
if (is_dir($dir)){  <-- ***THIS MIGHT BE FAILING***
    if ($dh = opendir($dir)){ <-- ***THIS MIGHT BE FAILING***
        while (($file = readdir($dh)) !== false){ <-- ***THIS MIGHT BE FAILING***
            $file_info = pathinfo($file);
            $output_string['worked'] = 'found <span style="color:yellowgreen;">' . $file . '</span><br />';
            echo json_encode($output_string);
        }
        closedir($dh);
    }
}

所以,你只需要在这些条件中解释错误的结果:

// read contents from $dir
if (is_dir($dir)){  <-- ***THIS MIGHT BE FAILING***
    if ($dh = opendir($dir)){ <-- ***THIS MIGHT BE FAILING***
        while (($file = readdir($dh)) !== false){ <-- ***THIS MIGHT BE FAILING***
            $file_info = pathinfo($file);
            $output_string['worked'] = 'found <span style="color:yellowgreen;">' . $file . '</span><br />';
            echo json_encode($output_string);
        }
        closedir($dh);
    } else {
      exit(json_encode(array('status' => 'Cannot open the $dir'))); <-- EXIT WITH FAIL
    }
} else {
  exit(json_encode(array('status' => '$dir is not a dir'))); <-- EXIT WITH FAIL
}

这至少应该让你知道什么是失败的。。。祝你好运。:)