【问题标题】:How to save file after it had been edited with jquery using php?使用php使用jquery编辑文件后如何保存文件?
【发布时间】:2012-07-23 03:35:53
【问题描述】:

在这里我创建了一个 jquery 函数,它获取 css-color 并创建具有 css-color 背景的元素:Edit my jsFiddle

html

<div ID="wrapper">
    <div ID="addColor">
        <input type="text" ID="hex">
        <div ID="color">Your color</div>
        <button ID="add">Add color</button>
    <div CLASS="clear"></div> <!-- Clear float -->
    </div>

    <div ID="wrapGallery">
        <h1>My Color Gallery</h1>
        <ul ID="gallery"></ul>
    </div>
</div>

js/jquery

$(function() {
    //float left with some margin
    $('#addColor')
        .children().not('#add, .clear').css({
        'float':'left',
        'margin-right': '5px'
        });

    //Showing color on keyup
    $('#hex').keyup(function() {
       var hexCode = $(this).val();
       $('#color').css('background-color', hexCode);

        if ( hexCode !== '') {
            $('#color').text('');
        }else{
            $('#color').text('Your color');
        }

    });

    //Adding colors
    $gallery = $('#gallery');

    $('#add').click(function() {

        var storedHex = $('#hex').val();
        //check if empty
        if (storedHex == '') {
            alert('Enter something');        
        }
        else {
        //adding li
        $("<li>").css('background-color', storedHex)
                .hover(
                  function () {
                    $(this).text(storedHex);
                  }, 
                  function () {
                    $(this).text('');
                  })                    
                 .appendTo($gallery); 
        }
    });
});

我唯一需要做的就是将创建的元素永久保存在文件中,以便我可以随时访问。我不知道该怎么做。

【问题讨论】:

  • 将它们永久保存到什么文件?
  • @DavidStetler 我尝试使用表单传递值并回显该变量,但您知道如何将该新变量永久保存在同一个文件或任何其他方式上吗?

标签: php javascript jquery ajax json


【解决方案1】:

目前,Javascript(在浏览器中)无法按照您的意愿编辑/保存文件。但是,您可以使用 .ajax() 将数据发送回 PHP 文件,以便 PHP 文件可以使用 file_put_contents() 保存它(尽管它通常会将其保存在数据库中)。

【讨论】:

    【解决方案2】:

    阅读 JQuery Ajax 函数并阅读 JSON 对象。

    使用 AJAX 和 JQuery,您可以轻松地将 JSON 对象发送到您的服务器,如下所示:

    function saveElements(myJsonObj) {
      $.ajax({
        method: "POST",
        dataType: "json",
        url: "path/to/savemyobject.php",
        data: {json:myJsonObj}
      });
    }
    

    JSON 文件可能如下所示:

    { "myData":
      {
        "some_text":"this could be the CSS code",
        "name":"some name",
        "number":"some number"
      }
    }
    

    您可以生成一个表示 JSON 对象的 JSON 字符串,然后使用 Ajax 函数(如上)将其发送到 PHP 文件。

    在 PHP 中,您可以这样做来获取对象:

    <?php
    $json_string = $_POST["json"]; //the same variable key we sent it in
    $json_obj = json_decode($json_string, true);
    // get data
    $myDataCode = $json_obj["myData"]["some_text"];
    // do some processing
    ...
    ?>
    

    您还可以使用 PHP json_encode 函数将 PHP 中的数组转换为 JSON 对象。

    阅读更多:
    http://www.w3schools.com/json/default.asp
    http://php.net/manual/en/function.json-decode.php
    http://php.net/manual/en/function.json-encode.php
    http://api.jquery.com/jQuery.ajax/

    【讨论】:

    • 如果我传递变量并想保存在文件上怎么办?
    • data:{json:myJsonObj, variable:"another var"} 但我不明白为什么你不能将变量与 JSON 对象 @PhoxKiD 一起放入
    猜你喜欢
    • 1970-01-01
    • 2014-04-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2011-11-01
    • 2016-01-04
    • 1970-01-01
    相关资源
    最近更新 更多