jQuery的AJAX保存到文件 [英] Jquery AJAX Save to file

查看:149
本文介绍了jQuery的AJAX保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部的寄托在这里工作得很好,只是保存的文件中它并没有给我整个字符串。只是ID之一(有多个页面上)。

不知道如何得到所有的ID和内容中的$。阿贾克斯()

我是什么做错了吗?

有没有这种jQuery的:

  $('A#exportPage')。在('点击',函数(){
变种contentMap = {};
$([编号^ =appendHeading]')。每个(函数(){
    contentMap [this.id] = $(本)的.text();
});
对于(ID在contentMap)
    $(#PrintIds)追加(对象ID:+编号+内容:+ contentMap [ID]);

$阿贾克斯({
  网址:post.php中,
  类型:后,
  数据: {
      OBJECTID:身份证,
      内容:contentMap [ID]
      },
      成功:函数(){
      警报(成功);
  },
  错误:函数(){
      警报(失败);
  }
 });
});
 

和这个PHP:

 < PHP
如果($ _ POST ['对象ID'] || $ _ POST ['内容']){
$ MYFILE =test.css;
$ StringData是= $ _ POST ['对象ID'] || $ _ POST ['内容'];
file_put_contents($ MYFILE,$ StringData是);
}
?>
 

解决方案

您忘了包围的循环进入 {}

  $('A#exportPage')。在('点击',函数(){
变种contentMap = {};
$([编号^ =appendHeading]')。每个(函数(){
    contentMap [this.id] = $(本)的.text();
});
对于(ID在contentMap){
    $(#PrintIds)追加(对象ID:+编号+内容:+ contentMap [ID]);

    $阿贾克斯({
        网址:post.php中,
        类型:后,
        数据: {
            OBJECTID:身份证,
            内容:contentMap [ID]
        },
        成功:函数(){
            警报(成功);
        },
        错误:函数(){
            警报(失败);
        }
    });
}
});
 

的内容应被附加到文件的末尾:

 < PHP
如果($ _ POST ['对象ID'] || $ _ POST ['内容']){
    $ MYFILE =test.css;
    $ StringData是= $ _ POST ['OBJECTID。 :。 $ _ POST ['内容'。 \ N的;
    file_put_contents($ MYFILE,$ StringData是FILE_APPEND | LOCK_EX);
}
?>
 

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

解决方案

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'] . "\n";
    file_put_contents($myFile,$stringData,FILE_APPEND | LOCK_EX);
}
?>

这篇关于jQuery的AJAX保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆