简体   繁体   English

如何使用 JS 从 php 表单中获取值并将其与其他文本一起填充到另一个字段中?

[英]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.

相关问题 如何从一种形式的输入字段中获取数据,并使用JQuery使用它以另一种形式填充相同的输入类型? - How can I take data from one an input field of one form and use it to populate the same input type in a different form using JQuery? 如何在表单查找期间将隐藏的 SQL ID 字段值与其他数据一起传递? - How can I pass a hidden SQL ID field value alongside other data during a form lookup? 如何根据其他四个字段的值填充字段? - How can I populate a field dependent on the values from four other fields? 如何从用户的输入文本字段中获取多个值 - How can I take mulitple values in input text field from user 如何从 JavaScript 中的元素列表填充表单字段? - How can I populate a form field from a list of elements in JavaScript? 如何使用prototype.js在html表单的文本字段内获取值? - How do I take value inside a text field of a html form using prototype.js? 我可以使用“this”从按钮中获取文本吗? - Can I use 'this' to take text from a button? 我可以在js中与其他一些功能一起倒计时吗? - Can i make a countdown alongside some other functio in js? 如何使用jQuery-UI datepicker获取表单输入字段以使用其他字段中的年份? - How can I get a form input field using jQuery-UI datepicker to use the year from another field? 如何将另一个组件中的值填充到redux表单中? - How to populate the values from another component into the redux form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM