javascript - 在电子邮件主题中添加生成的 ID 号

标签 javascript php html

我的网站和 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/

相关文章:

PHP/AJAX 并发 session 仅在 Chrome 中行为不端

c# - 如何在 c sharp 中以编程方式将 html 标签 dir 属性从 rtl 设置为 ltr,反之亦然

html - 如何用元素覆盖一 block 边框?

javascript - 如何在 Mozilla Firefox 中禁用表单?

JavaScript - 如何创建原型(prototype)函数来打印所选元素中的文本?

javascript - 是否仅使用 mongodb 1.4 及更早版本运行 mongoskin?

javascript - 如何在 Canvas 中绘制单个设备像素

php - MySql Select 语句不起作用

php - MySQL "FIELD_IN_SET()"相当于 "joined"表

javascript - 使用 Firestore 显示简单的博客文章