[英]Generate Unique Code/ID In Contact form ? PHP/Ajax
Hello So I have my contact form and everything is working fine What I want to do now is have the PHP Generate a unique code/id for example "002302103" and attach it to the email when the email is submitted. 您好,所以我有联系表格,一切正常,我现在想要做的就是让PHP生成唯一的代码/标识,例如“ 002302103”,并在提交电子邮件时将其附加到电子邮件中。 Basically a random reference number that is generated each time the form is used and that random set of numbers is also sent with the email.
基本上,每次使用表格时都会生成一个随机参考号,并且该随机数集也会与电子邮件一起发送。 Does anyone know how I go about this ?
有人知道我该怎么做吗?
Here is my php code: 这是我的PHP代码:
<?php
if($_POST)
{
$to_Email = "MyEmail@Email.com";
$subject = 'Message from my website'; //Subject line for emails
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$user_Message = str_replace("\'", "'", $user_Message);
$user_Message = str_replace("'", "'", $user_Message);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($user_Message)<5) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sentMail = @mail($to_Email, $subject, $user_Message . "\r\n\n" .'-- '.$user_Name. "\r\n" .'-- '.$user_Email, $headers);
if(!$sentMail)
{
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .'! Thank you for your email'));
die($output);
}
}
?>
Thank you in advance for helping/pointing me in the right direction 在此先感谢您帮助/指出正确的方向
Using time()
as mentioned will give you a sortable way to create unique IDs. 如前所述,使用
time()
将为您提供一种创建唯一ID的可排序方式。 Concatenating strings will also further randomize your desired result and still keep it sortable: 串联字符串还可以进一步随机化所需的结果,并保持其可排序性:
$uniqueId= time().mt_rand();
Try something like this: 尝试这样的事情:
echo mt_rand() . "\n";
echo mt_rand() . "\n";
Its output is: 其输出为:
1604716014
1478613278
mt_rand() Reference: mt_rand()参考:
Append this to your message like: 将此附加到您的消息,例如:
$subject = 'Message from my website - '.$mt_rand();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.