简体   繁体   English

jQuery和AJAX用于txt文件

[英]Jquery and AJAX for txt file

This is the script I have 这是我的脚本

<div id="result"></div>
<script src="jquery-1.10.2.min.js"></script>
<script>
$('#year li').click(function() {
    var text = $(this).text();
    //alert('text is ' + text);
    $.post("B.php",
    {
    text: text,

      } );  
    event.preventDefault() ;
  });
</script>

it executes a php script when a list item is clicked. 单击列表项时,它将执行php脚本。 The script B.php creates a few txt files. 脚本B.php创建了一些txt文件。 lets call one of them ddd.txt . 让我们叫ddd.txt之一 Now I want to load the contents of the file ddd.txt onto the div with id result when the click has been made. 现在,我想在单击时将ddd.txt文件的内容加载到具有id 结果的div上。 Can someone help me with it? 有人可以帮我吗?

In your php file add the following code 在您的php file添加以下代码

$file = 'ddd.txt';
$contents = file($file); 
$string = implode($contents); 
echo $string;

In your post success add the you will get this value that is being echoed from the php file .suppose it is named data post success添加后,您将获得从php文件echoed该值。假设它名为data

$('#result').text(data);

Hope this helps u mate.. :) 希望这可以帮助你交配.. :)

$.post("B.php",{ text: text}, function(data){
    $('#result').text(data);
}); 

Or if jQuery >= 1.5: 或者如果jQuery> = 1.5:

$.post("B.php",{ text: text}).done(function(data){
    $('#result').text(data);
}).fail(function(){
    console.log('something went wrong', arguments);
}); 

You have two options. 您有两个选择。 The "syncronous" method where you load the results in the generator request. 您可以在生成器请求中加载结果的“同步”方法。 Or the "asyncronous" method, that you check for results every x seconds after the generator runs. 或“异步”方法,您在生成器运行后每x秒检查一次结果。

Syncronously you could do 同步地你可以做

$.post("B.php, { 'text': text }, function(return_data, txtStatus, jqXHR) {
    $('#result').html(return_data);
});

and have your php generate the file(s), and output the data you want to display before ending the script. 并让您的php生成文件,并在结束脚本之前输出要显示的数据。

Asyncronously, there are several options. 异步地,有几种选择。 The easiest method is to use $().load , where you would do: 最简单的方法是使用$().load ,您可以在其中执行以下操作:

function check_results(text) {
    $('#results').load('somescript.php', { 'text' : text }, function(return_data) {
        if(return_data == "") {
            setTimeout(function(){ check_results(text); }, 1000);
        }
    });
}


$.post("B.php, { 'text': text }, function(return_data, txtStatus, jqXHR) {
    check_results(text);
});

Essentially, as soon as the POST returns, you start checking for files. 实质上,一旦POST返回,便开始检查文件。 If this script generates lots of files and takes some time, but you want to get, for example, the first file, you could run the check results function in-tandem with the post by calling it outside of the post callback function (which is more true to being asyncronous i guess). 如果此脚本生成大量文件并花费一些时间,但是您想获取例如第一个文件,则可以通过在post回调函数之外调用它来与post串联运行check results函数(这是我猜是更异步的。

Obviously the code is an example, so take with a pinch of salt, you will eed to modify it. 显然,该代码是一个示例,因此,请捏一点盐,然后对其进行修改。

I would do this complete action via $.ajax(). 我将通过$ .ajax()完成此完整操作。

$('#year li').click(function(){
    var text = $(this).text();
    $.ajax({url: 'B.php', type: 'POST', data: {'value':text}, success: function(data){
        alert(data); // alert any return values;
    }
    });
});

And in B.php you can use the $_POST['value'] variable to post the contents to the file, with it you can also add whatever content you like in clean php code. 在B.php中,您可以使用$ _POST ['value']变量将内容发布到文件中,也可以在干净的php代码中添加所需的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM