dqw7121 2013-04-28 01:51
浏览 504
已采纳

Jquery AJAX保存到文件

Everthing here works fine except that in the saved file it doesn't give me the whole string. Just one of the IDs (There are multiple on the page).

Not sure how get "all of the IDs and Content" in the $.ajax()

What am I doing wrong?

Have got this jquery:

$('a#exportPage').on('click',function(){
var contentMap = {};
$('[id^="appendHeading"]').each(function(){
    contentMap[this.id] = $(this).text();
});
for(id in contentMap)
    $("#PrintIds").append("ObjectID:" + id + "Content:" + contentMap[id]);

$.ajax({
  url: "post.php",
  type: "post",
  data: { 
      objectID: id,
      content: contentMap[id]
      },
      success: function(){
      alert("success");
  },
  error:function(){
      alert("failure");
  }   
 }); 
});

And this PHP:

<?php
if ($_POST['objectID'] || $_POST['content']) {
$myFile = "test.css";
$stringData = $_POST['objectID'] || $_POST['content'];
file_put_contents($myFile,$stringData);
}
?>
  • 写回答

1条回答 默认 最新

  • doujiyun0041 2013-04-28 02:13
    关注

    You forgot to enclosed the for loop into { and }.

    $('a#exportPage').on('click',function(){
    var contentMap = {};
    $('[id^="appendHeading"]').each(function(){
        contentMap[this.id] = $(this).text();
    });
    for(id in contentMap) {
        $("#PrintIds").append("ObjectID:" + id + "Content:" + contentMap[id]);
    
        $.ajax({
            url: "post.php",
            type: "post",
            data: { 
                objectID: id,
                content: contentMap[id]
            },
            success: function(){
                alert("success");
            },
            error:function(){
                alert("failure");
            }   
        }); 
    }
    });
    

    The content should be appended to the end of the file:

    <?php
    if ($_POST['objectID'] || $_POST['content']) {
        $myFile = "test.css";
        $stringData = $_POST['objectID'] . ':' . $_POST['content'] . "
    ";
        file_put_contents($myFile,$stringData,FILE_APPEND | LOCK_EX);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题