创建适用于两个独立脚本的随机 ID
Creating randomized ID that applies for two separate scripts
我不确定如何解决这个问题,所以希望你们中的一些人有一个很好的解决方案。
我设置了两份联系表 - 一份用于提供初始信息,另一份用于提供相关的后续问题。请注意,它是有意发送两种不同的表格的。
当通过电子邮件接收信息时,我想要一个随机 ID,例如:#42432,以表明收到的两封电子邮件来自同一个 session/person。
<!-- Part 1 -->
<script>
function send1() {
var form_name = jQuery('[name=name]').val();
var form_title = jQuery('[name=title]').val();
var form_email = jQuery('[name=email]').val();
var form_textarea = jQuery('[name=textarea]').val();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contact-form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "php/contact1.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("name="+form_name+"&title="+form_title+"&email="+form_email+"&textarea="+form_textarea);
}
</script>
<!-- Part 2 -->
<script>
function send2() {
var form_industry = jQuery("[name='industry']").is(":checked");
var form_academia = jQuery("[name=academia]").is(":checked");
var form_targeted = jQuery("[name=targeted]").is(":checked");
var form_discovery = jQuery("[name=discovery]").is(":checked");
var form_dunno = jQuery("[name=dunno]").is(":checked");
var form_samples = jQuery('[name=samples]').val();
var form_groups = jQuery('[name=groups]').val();
var form_groups2 = jQuery('[name=groups2]').val();
var form_biological = jQuery('[name=biological]').val();
var form_technical = jQuery('[name=technical]').val();
var form_source = jQuery('[name=source]').val();
var form_origen = jQuery('[name=origen]').val();
var form_extraction = jQuery('[name=extraction]').val();
var form_aim = jQuery('[name=aim]').val();
var form_phone = jQuery('[name=phone]').val();
var form_affiliated = jQuery('[name=affiliated]').val();
var form_textarea = jQuery('[name=textarea]').val();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contact-form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "php/contact2.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("industry="+form_industry+"&academia="+form_academia+"&targeted="+form_targeted+"&discovery="+form_discovery+"&dunno="+form_dunno+"&samples="+form_samples+"&groups="+form_groups+"&groups2="+form_groups2+"&biological="+form_biological+"&technical="+form_technical+"&source="+form_source+"&origen="+form_origen+"&extraction="+form_extraction+"&aim="+form_aim+"&phone="+form_phone+"&affiliated="+form_affiliated+"&textarea="+form_textarea);
}
</script>
xhttp.open("POST", "php/contact1.php" 的例子:
<?php
$name = $_POST['name'];
$title = $_POST['title'];
$email = $_POST['email'];
$textarea = $_POST['textarea'];
$email_from = 'New contact';
$email_subject = 'New Contact - Request with Questionary';
$email_body = "New contact request (Request with Questionary):
\n".
"Name: $name\n".
"Title: $title\n".
"Email: $email\n".
"Message: $textarea\n";
$to = "email@email.com";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("location: https://website.com/php/questionary.html");
?>
第二种形式的 PHP 文件以相同的方式构建。
期待听到您的解决方案。
一切顺利,
路易斯
您可以像 answer 中提到的那样生成 GUID 或随机数作为全局变量,如果在同一页面中加载表单而无需重新加载,这将在两种表单中都可用。例如:
<script>
function uuidv4() {
const a = crypto.getRandomValues(new Uint16Array(8));
let i = 0;
return '00-0-4-1-000'.replace(/[^-]/g,
s => (a[i++] + s * 0x10000 >> s).toString(16).padStart(4, '0')
);
}
var guidVariable = uuidv4();
function send1() {
...
xhttp.open("POST", "php/contact1.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("name="+form_name+"&title="+form_title+"&email="+form_email+"&textarea="+form_textarea+"&guidVariable ="+guidVariable );
}
</script>
<!-- Part 2 -->
<script>
function send2() {
...
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contact-form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "php/contact2.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("industry="+form_industry+"&academia="+form_academia+"&targeted="+form_targeted+"&discovery="+form_discovery+"&dunno="+form_dunno+"&samples="+form_samples+"&groups="+form_groups+"&groups2="+form_groups2+"&biological="+form_biological+"&technical="+form_technical+"&source="+form_source+"&origen="+form_origen+"&extraction="+form_extraction+"&aim="+form_aim+"&phone="+form_phone+"&affiliated="+form_affiliated+"&textarea="+form_textarea+"&guidVariable ="+guidVariable );
}
</script>
引用How to create a GUID / UUID
我不确定如何解决这个问题,所以希望你们中的一些人有一个很好的解决方案。
我设置了两份联系表 - 一份用于提供初始信息,另一份用于提供相关的后续问题。请注意,它是有意发送两种不同的表格的。
当通过电子邮件接收信息时,我想要一个随机 ID,例如:#42432,以表明收到的两封电子邮件来自同一个 session/person。
<!-- Part 1 -->
<script>
function send1() {
var form_name = jQuery('[name=name]').val();
var form_title = jQuery('[name=title]').val();
var form_email = jQuery('[name=email]').val();
var form_textarea = jQuery('[name=textarea]').val();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contact-form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "php/contact1.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("name="+form_name+"&title="+form_title+"&email="+form_email+"&textarea="+form_textarea);
}
</script>
<!-- Part 2 -->
<script>
function send2() {
var form_industry = jQuery("[name='industry']").is(":checked");
var form_academia = jQuery("[name=academia]").is(":checked");
var form_targeted = jQuery("[name=targeted]").is(":checked");
var form_discovery = jQuery("[name=discovery]").is(":checked");
var form_dunno = jQuery("[name=dunno]").is(":checked");
var form_samples = jQuery('[name=samples]').val();
var form_groups = jQuery('[name=groups]').val();
var form_groups2 = jQuery('[name=groups2]').val();
var form_biological = jQuery('[name=biological]').val();
var form_technical = jQuery('[name=technical]').val();
var form_source = jQuery('[name=source]').val();
var form_origen = jQuery('[name=origen]').val();
var form_extraction = jQuery('[name=extraction]').val();
var form_aim = jQuery('[name=aim]').val();
var form_phone = jQuery('[name=phone]').val();
var form_affiliated = jQuery('[name=affiliated]').val();
var form_textarea = jQuery('[name=textarea]').val();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contact-form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "php/contact2.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("industry="+form_industry+"&academia="+form_academia+"&targeted="+form_targeted+"&discovery="+form_discovery+"&dunno="+form_dunno+"&samples="+form_samples+"&groups="+form_groups+"&groups2="+form_groups2+"&biological="+form_biological+"&technical="+form_technical+"&source="+form_source+"&origen="+form_origen+"&extraction="+form_extraction+"&aim="+form_aim+"&phone="+form_phone+"&affiliated="+form_affiliated+"&textarea="+form_textarea);
}
</script>
xhttp.open("POST", "php/contact1.php" 的例子:
<?php
$name = $_POST['name'];
$title = $_POST['title'];
$email = $_POST['email'];
$textarea = $_POST['textarea'];
$email_from = 'New contact';
$email_subject = 'New Contact - Request with Questionary';
$email_body = "New contact request (Request with Questionary):
\n".
"Name: $name\n".
"Title: $title\n".
"Email: $email\n".
"Message: $textarea\n";
$to = "email@email.com";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("location: https://website.com/php/questionary.html");
?>
第二种形式的 PHP 文件以相同的方式构建。
期待听到您的解决方案。
一切顺利,
路易斯
您可以像 answer 中提到的那样生成 GUID 或随机数作为全局变量,如果在同一页面中加载表单而无需重新加载,这将在两种表单中都可用。例如:
<script>
function uuidv4() {
const a = crypto.getRandomValues(new Uint16Array(8));
let i = 0;
return '00-0-4-1-000'.replace(/[^-]/g,
s => (a[i++] + s * 0x10000 >> s).toString(16).padStart(4, '0')
);
}
var guidVariable = uuidv4();
function send1() {
...
xhttp.open("POST", "php/contact1.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("name="+form_name+"&title="+form_title+"&email="+form_email+"&textarea="+form_textarea+"&guidVariable ="+guidVariable );
}
</script>
<!-- Part 2 -->
<script>
function send2() {
...
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contact-form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "php/contact2.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("industry="+form_industry+"&academia="+form_academia+"&targeted="+form_targeted+"&discovery="+form_discovery+"&dunno="+form_dunno+"&samples="+form_samples+"&groups="+form_groups+"&groups2="+form_groups2+"&biological="+form_biological+"&technical="+form_technical+"&source="+form_source+"&origen="+form_origen+"&extraction="+form_extraction+"&aim="+form_aim+"&phone="+form_phone+"&affiliated="+form_affiliated+"&textarea="+form_textarea+"&guidVariable ="+guidVariable );
}
</script>
引用How to create a GUID / UUID