首页 > 解决方案 > ACE 编辑器:如何将更改的数据移动到 $_POST 或 $_GET

问题描述

我今天刚刚发现了 ACE 编辑器,我一直在使用它,它非常漂亮。虽然我不是 JavaScript 专家,但大部分功能都相当直观。我遇到的唯一问题是在完成文件编辑后如何将更改移动到 $_POST、$_GET 或 $_SESSION?您将看到我正在执行一个 PHP 文件以将更改保存到 MySQL 表中。

这是我的 HTML/JavaScript 代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>ACE in Action</title>
  <style type="text/css" media="screen">
    body {
      background-color: #ffff;
      color: #333;
      margin: 0;
      padding: 0;
    }
    
    .editor_container {
      text-align: center;
    }
    
    .editor_container form {
      margin: 45px;
      width: 90%;
      border: thin #333;
      border-radius: 8px;
      text-align: center;
    }
    
    .ace_gutter-cell {
      color: white;
      !important
    }
  </style>
</head>

<body>
  <div class="editor_container">
    <h1>Ace Editor</h1>
    <form action="update.php" method="post">
      <h2>Edit the CSS file</h2>
      <div id="editor" title="css edit:" style="height: 500px; width: 80%; float: right;">
        .wrdAdmin { border-radius: 8px; border: solid thin #333; margin: auto; padding: 40px; position: relative; /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#ffffff+0,ededed+100 */ background: #ffffff; /* Old browsers
        */ background: -moz-linear-gradient(top, #ffffff 0%, #ededed 100%); /* FF3.6-15 */ background: -webkit-linear-gradient(top, #ffffff 0%,#ededed 100%); /* Chrome10-25,Safari5.1-6 */ background: linear-gradient(to bottom, #ffffff 0%,#ededed 100%);
        /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 ); /* IE6-9 */ } .wrdAdmin h1 { font-size: 18pt; font-weight: bold; padding-bottom:
        12px; } .wrdAdmin h2 { font-size: 16pt; font-weight: bold; padding-bottom: 10px; } .wrdAdmin h3 { font-size: 14pt; font-weight: bold; padding-bottom: 8px; } .wrdAdmin h4 { font-size: 12pt; font-weight: bold; padding-bottom: 6px; } .wrdAdmin hr
        { border: thin solid #333; margin-bottom: 12px; }
      </div>
      <br><br>
      <input type="submit" value="Submit">
    </form>
  </div>

  <script src="/Ace/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
  <script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/ambiance");
    editor.session.setMode("ace/mode/css");
    document.getElementById('editor').style.fontSize = '1vw';
  </script>
</body>

</html>

标签: javascripthtmlace-editor

解决方案


<input type="textarea">向您form的隐藏字段添加一个字段。添加一个 JavaScript 处理程序,用于检测何时提交表单。此处理程序应将隐藏输入字段的值设置为 ACE 编辑器的当前值。然后您可以从 PHP 中读取输入字段的值。

这是您修改后的代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>ACE in Action</title>
  <style type="text/css" media="screen">
    body {
      background-color: #ffff;
      color: #333;
      margin: 0;
      padding: 0;
    }

    .editor_container {
      text-align: center;
    }

    .editor_container form {
      margin: 45px;
      width: 90%;
      border: thin #333;
      border-radius: 8px;
      text-align: center;
    }

    .ace_gutter-cell {
      color: white;
      !important
    }
  </style>
</head>

<body>
  <div class="editor_container">
    <h1>Ace Editor</h1>
    <form action="update.php" method="post" id="myForm">  <!-- ID added here -->
      <input name="foo" type="text" hidden id="editortext"> <!-- hidden text field here added here-->
      <h2>Edit the CSS file</h2>
      <div id="editor" title="css edit:" style="height: 500px; width: 80%; float: right;">
        .wrdAdmin { border-radius: 8px; border: solid thin #333; margin: auto; padding: 40px; position: relative; /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#ffffff+0,ededed+100 */ background: #ffffff; /* Old browsers
        */ background: -moz-linear-gradient(top, #ffffff 0%, #ededed 100%); /* FF3.6-15 */ background: -webkit-linear-gradient(top, #ffffff 0%,#ededed 100%); /* Chrome10-25,Safari5.1-6 */ background: linear-gradient(to bottom, #ffffff 0%,#ededed 100%);
        /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 ); /* IE6-9 */ } .wrdAdmin h1 { font-size: 18pt; font-weight: bold; padding-bottom:
        12px; } .wrdAdmin h2 { font-size: 16pt; font-weight: bold; padding-bottom: 10px; } .wrdAdmin h3 { font-size: 14pt; font-weight: bold; padding-bottom: 8px; } .wrdAdmin h4 { font-size: 12pt; font-weight: bold; padding-bottom: 6px; } .wrdAdmin hr
        { border: thin solid #333; margin-bottom: 12px; }
      </div>
      <br><br>
      <input type="submit" value="Submit">
    </form>
  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/ace.js" type="text/javascript" charset="utf-8"></script>
  <script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/ambiance");
    editor.session.setMode("ace/mode/css");
    document.getElementById('editor').style.fontSize = '1vw';

    // added event handler
    document.getElementById("myForm").onsubmit = function(evt) {
      document.getElementById("editortext").value = editor.getValue();
    }
  </script>
</body>

</html>

推荐阅读