[英]How can I use JS to take the values from a php form and populate it into another field alongside other text?
I'm using php to create a form that has a field for the first name, a field for the last name, and a hidden field called "Subject" (that will be used for an email upon submission).我正在使用 php 创建一个表单,该表单具有一个字段用于名字,一个字段用于姓氏,以及一个名为“主题”的隐藏字段(将在提交时用于电子邮件)。 I wanted to know how I can use js to pull the values from the two fields and populate it into the Subject field in this way "My form - [First and last name]" in a cross-browser, yet simple way?
我想知道如何使用 js 从两个字段中提取值并以这种方式“我的表单 - [名字和姓氏]”以跨浏览器但简单的方式将其填充到主题字段中?
First Name and Last Name Fields:名字和姓氏字段:
<div class="small-12 large-6 cell">
<?php
$url="https://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
echo $api->input('source_url', array(
'type' => 'hidden',
'label' => false,
'value'=> $url
));
echo $api->input('00NF0000008rWcG', array(
'type' => 'text',
'label' => false,
'title' => 'First Name',
'required' => true,
'placeholder'=> false,
'aria-label'=>'First name',
'div'=> false,
'id'=> '00NF0000008rWcG',
'validation' => array('name'),
'validation_msg' => 'Please enter your first name',
)); ?>
<label for="00NF0000008rWcG" aria-hidden="true">First Name *</label>
</div>
<div class="small-12 large-6 cell">
<?php
$url="https://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
echo $api->input('source_url', array(
'type' => 'hidden',
'label' => false,
'value'=> $url
));
echo $api->input('00NF0000008rWcL', array(
'type' => 'text',
'label' => false,
'title' => 'Last Name',
'required' => true,
'placeholder'=> false,
'aria-label'=>'Last name',
'div'=> false,
'id'=> '00NF0000008rWcL',
'validation' => array('name'),
'validation_msg' => 'Please enter your last name',
)); ?>
<label for="00NF0000008rWcL" aria-hidden="true">Last Name *</label>
</div>
Hidden Subject Field:隐藏主题字段:
<div class="small-12 large-12 cell">
<?php
echo $api->input('subject', array(
'type' => 'hidden',
'value' => 'Form Name - ',
'id'=> 'subject',
));
?>
</div>
IDs have to start with a letter and cannot start with a number. ID 必须以字母开头,不能以数字开头。
The below code just attaches an event listener with a single function to both input fields with an updated ID.下面的代码只是将具有单个函数的事件侦听器附加到具有更新 ID 的两个输入字段。
let fName = document.querySelector("#A00NF0000008rWcL"); let lName = document.querySelector("#A00NF0000008rWcG"); let subject = document.querySelector("#subject"); let subjectSTR = ""; let subjectFormat = "My form - [{firstName} {lastName}]"; function buildSubject(){ subjectSTR = subjectFormat.replace("{lastName}",lName.value); subjectSTR = subjectSTR.replace("{firstName}",fName.value); subject.value = subjectSTR; } lName.addEventListener("input",buildSubject); fName.addEventListener("input",buildSubject);
<input type="text" id="A00NF0000008rWcL"><br> <input type="text" id="A00NF0000008rWcG"> <hr> <input type="text" id="subject">
In your case在你的情况下
PHP PHP
<div class="small-12 large-6 cell">
<?php
$url="https://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
echo $api->input('source_url', array(
'type' => 'hidden',
'label' => false,
'value'=> $url
));
echo $api->input('00NF0000008rWcG', array(
'type' => 'text',
'label' => false,
'title' => 'First Name',
'required' => true,
'placeholder'=> false,
'aria-label'=>'First name',
'div'=> false,
'id'=> '00NF0000008rWcG',
'oninput' => 'handleValueChange()',
'validation' => array('name'),
'validation_msg' => 'Please enter your first name',
)); ?>
<label for="00NF0000008rWcG" aria-hidden="true">First Name *</label>
</div>
<div class="small-12 large-6 cell">
<?php
$url="https://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
echo $api->input('source_url', array(
'type' => 'hidden',
'label' => false,
'value'=> $url
));
echo $api->input('00NF0000008rWcL', array(
'type' => 'text',
'label' => false,
'title' => 'Last Name',
'required' => true,
'placeholder'=> false,
'aria-label'=>'Last name',
'div'=> false,
'id'=> '00NF0000008rWcL',
'oninput' => 'handleValueChange()',
'validation' => array('name'),
'validation_msg' => 'Please enter your last name',
)); ?>
<label for="00NF0000008rWcL" aria-hidden="true">Last Name *</label>
</div>
JS JS
function handleValueChange(){
document.getElementById("subject").value="My form -["+document.getElementById("00NF0000008rWcG").value+document.getElementById("00NF0000008rWcL").value+"]";
}
For General Use一般用途
You can simply use and experiment with this example您可以简单地使用和试验这个例子
<input type="text" id="firstname" oninput="handleValueChange()">
<input type="text" id="lastname" oninput="handleValueChange()" >
<input type="hidden" id="subject" >
<script>
function handleValueChange(){
document.getElementById("subject").value="My form -["+document.getElementById("firstname").value+document.getElementById("lastname").value+"]";
}
</script>
For PHP对于 PHP
echo '
<input type="text" id="firstname" oninput="handleValueChange()">
<input type="text" id="lastname" oninput="handleValueChange()" >
<input type="hidden" id="subject" >
';
echo '
<script>
function handleValueChange(){
document.getElementById("subject").value="My form -["+document.getElementById("firstname").value+document.getElementById("lastname").value+"]";
}
</script>
';
Only jS只有js
function handleValueChange(){
document.getElementById("subject").value="My form -["+document.getElementById("firstname").value+document.getElementById("lastname").value+"]";
}
I hope it will help我希望它会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.