Save content of a div to new file with jQuery AJAX and PHP

拈花ヽ惹草 提交于 2019-12-20 02:40:08

问题


I'm trying to save contents of a div to new html file. I'm using jQuery AJAX to send data to php.
However, the 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";
}

?>

回答1:


Request method property is type instead of method ($.ajax({ method : "POST" is actually $.ajax({ type : "POST") and $_POST['data'] should be $_POST['id'].




回答2:


There is no $_POST['data'] being posted, only $_POST['id']. Look at this part:

data: {id : bufferId},


来源:https://stackoverflow.com/questions/9758738/save-content-of-a-div-to-new-file-with-jquery-ajax-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!