使用PHP或JavaScript将属性连接到属性

I have the following part of code in my file.

<?php
$output = "<font id=''>" . $_POST["input"] . "</font>";
file_put_contents("output.html", $output, FILE_APPEND);
?>

The user can submit the form several times, and each time, the value entered in the input-field will be saved into a file. What I want to do, either with PHP or JavaScript, is to add consecutive numbers in the id attribute for each value submitted.

Example: user enters hello and submits form. Only <font id=''>hello</font> will be written into the file. What I want to do is to write 1 in the id attribute for the first value submitted (<font id='1'>hello</font>). For the second value, to be written 2, 3 for the third value, etc.

Is it possible to do this? If so, how?

Thank you.

It's difficult have a count of submit, because when you do the submit, a variable in javascript will lose the counter, and in php too.

So my idea it's use AJAX to do the submit, and you have a counter in Javascript that it starts at 1. When submit, increment this counter and pass the counter like other input hidden.

In summary, I have a form with 2 inputs, the one you have, and other hidden in which you put the counter of submits. You do the submit using ajax, and the page not reload. So you can have the counter to add in the file like other $_POST['counter'].

Try this and tell me if this be useful.

Depending on if you need to remember the number for all users, or just the one user, you could use PHP sessions.

You'll need to run session_start at the beginning of the script, but after that the variables inside $_SESSION will keep living until the user's browser session ends (configurable in the php.ini).

<?php
    session_start();
    if(!isset($_SESSION['post_counter']) {
        $_SESSION['post_counter'] = 0;
    }

    $_SESSION['post_counter']++;

    $output = "<font id='" + $_SESSION['post_counter'] + "'>" . $_POST["input"] . "</font>";
    file_put_contents("output.html", $output, FILE_APPEND);
?>