我有一个变量,我需要发送到 php 才能写入文件


I have a variable that i need to send to php to be written to a file

我有一个特定的数组,php 需要访问和写入文件。我还希望能够调用 php 来获取数组信息。我使用 JSON.strigify 将数组存储在字符串中,但我无法弄清楚如何使用 php 将其发送到服务器。我几乎没有 php 经验,我尝试过:

<script language="javascript">

var COMMENTS_FOR_DISPLAY = new Array('Have fun with this code: Chris');
// Adds a new comment, name pair to the Array feeding textualizer.
function add_comment() {
  // Retrieve values and add them to Array.
  var new_comment = $('#kwote').val();
  var new_name = $('#name').val();

   COMMENTS_FOR_DISPLAY.push(new_comment + ': ' + new_name);

  // Reset <input> fields.
  $('#kwote').val('');
  $('#name').val('');
  var arrayAsString = JSON.stringify(COMMENTS_FOR_DISPLAY);
}

$(document).ready(function() {
  var txt = $('#txtlzr');  // The container in which to render the list
  var options = {
    duration: 5,          // Time (ms) each blurb will remain on screen
    rearrangeDuration: 5, // Time a character takes to reach its position
    effect: 'random',     // Animation effect the characters use to appear
    centered: true        // Centers the text relative to its container
  }
  txt.textualizer(COMMENTS_FOR_DISPLAY); // textualize it!
  txt.textualizer('start'); // start
});
</script>

在主要.php我把:

<?php
$kwoteString = $_GET["arrayAsString"];
echo $kwoteString;
?>

我使用 echo 来查看我是否得到了任何输出,但我没有。这可能是一个非常简单的修复,也许我缺少一个标题或告诉我的 html 文档阅读 main.php??任何帮助将不胜感激!

将 jquery 与$.post(url,params);

网络上有很多教程,堆栈溢出本身。

这是文档:http://api.jquery.com/jQuery.post/

您可以添加一个隐藏字段并将字符串设置为隐藏字段。PHP 代码将从隐藏字段中读取值。