使用ajax写入文件?

i have a textarea and a save button..I want to capture the data in the textarea and write it to a file but am getting problems..Am getting problems in writing the code for ajax.Note i am not using form.

    <textarea  id="display" name="display" style="overflow:auto;resize:none"     rows="35" cols="40"><?php echo $data;?></textarea>



       <input type="button" name="Save" value="Save" onclick="save_data()"/>

    This is the Ajax i wrote...
    function save_data(){

            var ajaxadd;
            ajaxadd=new XMLHttpRequest();
            var url = "save.php";
            ajaxadd.open("GET",url,true);
            ajaxadd.onreadystatechange = function(){
                if(ajaxadd.readyState==4){
                    document.getElementById('display').value = ajaxadd.responseText;
                }
            }
            ajaxadd.send(null);
            alert("Item successfully ADDED!");
        }


    This is the save.php
    <?php
    if(!empty($_POST['data'])){
        $data = $_POST['data'];
        $fname ="XHTML_file.xhtml";

        $file = fopen($fname, 'w');//creates new file
        fwrite($file, $data);
        fclose($file);
    }
?>

You are making a GET request:

ajaxadd.open("GET",url,true);

But you are expecting a POST request:

if(!empty($_POST['data'])){

and you aren't sending any data:

ajaxadd.send(null);

First you are doing a get request and you are testing the post-variable at the server. Second: you never added the data from the text area to the request. You have to do that manually since you are not submitting a form

You are using the get method and not sending any data. Use post, and send the data:

function save_data(){
    var ajaxadd;
    ajaxadd=new XMLHttpRequest();
    var url = "save.php";
    ajaxadd.open("POST",url,true);
    ajaxadd.onreadystatechange = function(){
        if(ajaxadd.readyState==4){
            document.getElementById('display').value = ajaxadd.responseText;
        }
    }

    var textArea = document.getElementById('display');
    var formData = new FormData();
    formData.append("data", textArea.value); 

    ajaxadd.send(formData);
}