如何在按钮单击时编辑文件的内容? PHP? [英] How to edit the contents of a file on button click? PHP?

查看:72
本文介绍了如何在按钮单击时编辑文件的内容? PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

number.txt

1

index.php

<?php
$handle = file_get_contents("number.txt");    
?>

    <form action="" method="post">
    <button class="click"></button>
    </form>

我如何做到这一点,当用户点击按钮时,文件号码.txt 会被编辑,以便从1变为2?然后到3,4,5等等?

How would I make it so that when the user clicks on the button, the file number.txt gets edited so that it changes from 1 to 2? And then to 3, 4, 5 and so on?

点击按钮会将数字增加一个

Clicking on the button would increase the number by one

假设我点击了按钮,如果我打开 number.txt ,我现在会看到:

Let's say I clicked on the button, if I open up number.txt, I would now see:

2

重申一下,有一个名为 number.txt <的文件/ strong>并且该文件的内容为1(不带引号)。现在,在 index.php 页面上,有一个表单,其中包含按钮。单击按钮后,将重写 numbers.txt 文件,使其值增加1(每次单击按钮时) - 在这种情况下,文件 number.txt 现在将具有内容2(不带引号)。

Just to reiterate, there is a file called number.txt and that file has the contents "1" (without the quotes). Now, on the index.php page, there is a form containing a button within. Once you click on the button, numbers.txt file will be rewritten so that its value goes up by one (each time the button is clicked) - in this case, the file number.txt will now have the contents "2" (without the quotes).

我不太清楚如何做到这一点,所以感谢任何帮助。

I am not quite sure how I would do this, so any help is appreciated.

推荐答案

查看表单提交和处理文件(通过 fopen / fwrite / fclose 或通过 file_get_contents / file_put_contents )。这是您在上面描述的功能:

Look into form submitting and handling files (either via fopen/fwrite/fclose or via file_get_contents/file_put_contents). This is the functionality you described above:

<h2>Click</h2>
<form action="" method="post">
    <button name="click" class="click">Click me!</button>
</form>

<?php
if(isset($_POST['click']))
{
    $filename = "number.txt";
    $before_editing = file_get_contents($filename);
    echo "Content of the file " . $filename . " before editing: " . $before_editing . "<br>";
    file_put_contents($filename, "2");
    $after_editing = file_get_contents($filename);
    echo "Content of the file " . $filename . " after editing: " . $after_editing . "<br>";
}
?>

请注意,运行此脚本后,文件的内容为编号。 txt 被重写为2。你必须手动再次编辑才能看到一些变化。

Notice that once you run this script, the contents of the file number.txt gets rewritten to "2". You'll have to edit it again manually to see some changes.

根据问题的变化,这是每次单击按钮时将增加数值的代码:

Based on the change in the question, this is the code that will increment the number value each time the button gets clicked:

<h2>Click</h2>
<form action="" method="post">
    <button name="click" class="click">Click me!</button>
</form>

<?php
if(isset($_POST['click']))
{
    $filename = "number.txt";
    $content = file_get_contents($filename);
    preg_match("#\d+#", $content, $before_editing);
    $number_before = $before_editing[0];
    echo "The number in " . $filename . " before clicking: " . $number_before . "<br>";

    file_put_contents($filename, ++$number_before);

    $content = file_get_contents($filename);
    preg_match("#\d+#", $content, $after_editing);
    $number_after = $after_editing[0];
    echo "The number in " . $filename . " after clicking: " . $number_after . "<br>";
}
?>

这篇关于如何在按钮单击时编辑文件的内容? PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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