【问题标题】:php post to variable and then append to txt [closed]php发布到变量然后附加到txt [关闭]
【发布时间】:2014-02-11 21:03:07
【问题描述】:

我遇到了无法完成这个简单脚本的问题。基本上,我想从表单中获取帖子,将其分配给变量,然后将变量附加到预先存在的 example.txt 文件中。我为缺乏例子而道歉,我和我的姻亲出去吃饭后切换到我的手机。任何人都可以发布一个简单的例子并允许我建立这个基础吗?提前致谢

【问题讨论】:

  • 当你有机会回到桌面时,给我们一个你当前代码的例子。
  • 请先阅读如何提问页面,然后再询问您是否打算得到答案......

标签: php variables post append


【解决方案1】:

以下内容应该为您提供一个开始的基础。

<?php

// Check to see if the form was submitted

if(isset($_POST['action']) && $_POST['action'] == 'add'){

    $file = 'example.txt';

    // Open the file to get existing content
    $current = file_get_contents($file);

    // Append a new person to the file
    $current .= $_POST['test'];

    // Write the contents back to the file
    file_put_contents($file, $current);
}

?>

<form method="POST">

    <!-- This becomes PHP variable $_POST['test'] -->
    <input type="text" name="test" /> 

    <input type="hidden" name="action" value="add" />
    <input type="submit" value="Add"/>
</form>

查看http://us3.php.net/file_put_contents了解更多信息。

【讨论】:

  • 这很有魅力,非常感谢
【解决方案2】:

如果您的 html 表单有这样的文本字段 &lt;input type="text" name="firstName" value="Name"&gt; 您可以使用在您的 php 脚本中访问该值 $_POST['firstName'];. 如果你想在 php 中附加文本,你可以简单地使用“.”来完成。 例如:$post_string = 'Ex:' . $post_string;

【讨论】:

  • 哦!为什么投反对票.. :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
相关资源
最近更新 更多