dqw7121
2013-04-28 01:51
浏览 238

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);
}
?>

图片转代码服务由CSDN问答提供 功能建议

这里的Everthing工作正常,只是在保存的文件中它没有给我整个字符串。 只有一个ID(页面上有多个)。

不确定如何获取$ .ajax() 中的“所有ID和内容”

我做错了什么?

有这个jquery:

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

这个PHP:

 &lt;?php 
if($ _POST ['objectID'] |  | $ _POST ['content']){
 $ myFile =“test.css”; 
 $ stringData = $ _POST ['objectID'] ||  $ _POST ['content']; 
file_put_contents($ myFile,$ stringData); 
} 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    }
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题