Save div content to new file using jQuery AJAX and PHP

I am trying to save the content of a div to a new html file. I am using jQuery AJAX to post data to php.
However, php in its current form writes an empty file.

Html:

<div id="data2save">
     <span>data1</span>
     <span>data2</span>
     <span>data3</span>
     <span>data4</span>
</div>

<input type="button" value="save" id="save">

      

JQuery

 $("#save").live("click",function() {

    var bufferId =$("#data2save").html();

            $.ajax({
                 method : "POST",
                 url : "saver.php",
                 data: {id : bufferId},
                 dataType: "html",
                 success: function(data){ 
                 alert("ok");  
                 }
                 });
 });

      

PHP:

 <?php
$handle = fopen("test.html", 'w+');
$data = $_POST['data'];
if($handle)
{

if(!fwrite($handle, $data ))
echo "ok";
}

?>

      

+3


source to share


2 answers


The property of the request method type

instead of method

( $.ajax({ method : "POST"

actually $.ajax({ type : "POST"

) $_POST['data']

should be $_POST['id']

.



+5


source


No messages $ _POST ['data'], only $ _POST ['id']. Take a look at this part:



data: {id : bufferId},

      

+1


source







All Articles