简体   繁体   中英

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. The script B.php creates a few txt files. lets call one of them 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. Can someone help me with it?

In your php file add the following code

$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

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

Hope this helps u mate.. :)

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

Or if 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.

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.

Asyncronously, there are several options. The easiest method is to use $().load , where you would do:

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. 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).

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().

$('#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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