我的网站和 PHP 中有一个联系表单,我使用
$subject = $_POST['subject'];
将电子邮件的主题设为用户在 name='subject' 字段中输入的内容。
有什么方法可以在主题末尾添加 ID,以便每封电子邮件的 ID 号都会+1?
我猜测 PHP 需要将其存储在某处,以了解最后一个 ID 号是什么,但我不确定如何实现这一点。
完成后,我收到的电子邮件将包含以下主题:
user2354 typed subject [ID:000001]
user3456 typed subject [ID:000002]
(and so on ...)
最佳答案
如果你想用文件来存储id,可以使用这段代码:
<?php
$filename = __DIR__.'/id.txt'; // The file
if(!file_exists($filename)) { // File not exist start at 0
$id = 0;
}
else {
$id = file_get_contents($filename); // Get the id from the file
}
$id++; // Increment the id
file_put_contents($filename, $id); // Put the new id in the file
// The subject of the message
$subject = $user.' typed subject '.$_POST['subject'].' [ID:'.str_pad($id, 6, 0, STR_PAD_LEFT).']';
关于javascript - 在电子邮件主题中添加生成的 ID 号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54945968/