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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办