<p>You could either do this the JavaScript way (listen to onSubmit, grab DOM values, append if necessary, otherwise remove), or the PHP way (from a plugin).</p>

<p>I'll pseudocode the latter because I hate the former.</p>

<ol>
<li><p>Generate a Craft <a href="https://pluginfactory.io/" rel="nofollow">plugin</a>.</p></li>
<li><p>Have it <a href="https://craftcms.com/docs/plugins/hooks-and-events" rel="nofollow">listen</a> for the <a href="https://craftcms.com/docs/plugins/events-reference#entries-onBeforeSaveEntry" rel="nofollow">onBeforeSaveEntry</a> event.</p></li>
<li><p>Inside that event listener, you can check if the entry being saved belongs to the section you're interested in.</p></li>
<li><p>If so, check your Fetal Heart Rate field.</p></li>
<li><p>If its value is "bpm", set it to null or an empty string.</p></li>
<li><p>If it's anything other than "bpm", append " bpm" to its existing value.</p></li>
<li><p>Repeat 4-6 for the "Estimated Gestational Age" field.</p></li>
<li><p><a href="https://en.wikipedia.org/wiki/Bob%27s_your_uncle" rel="nofollow">Bob's your uncle</a>.</p></li>
</ol>

CC BY-SA 3.0
2016-09-28T18:32:35.617Z
Thanks for the steps @Brad.  Creating a plugin looks a bit out of my league so I'll research the javascript option.
Lori
CC BY-SA 3.0
2016-09-27T22:21:36.843Z
2016-09-27T22:21:36.843Z
Brad Bell
<p>I have a Matrix field in my Craft control panel which contains content which creates a front end form.  If the form field has a placeholder text entered in my Matrix field, I would like that text to be submitted along with the user's text input.</p>

<p><strong>If the user entered "100" in the front end form text input field, and my placeholder text is "bpm", the result I would like submitted is: "100 bpm".</strong></p>

<p>Here's my text input field form code:</p>

<pre><code>&lt;input type="{{ row.inputType }}" name="{{ block.fieldName }}" class="gui-input" placeholder="{{ row.label }}"&gt;
</code></pre>

<p>Here's a screenshot of my Matrix field:</p>

<p><a href="https://i.sstatic.net/BjJs1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BjJs1.png" alt="enter image description here"></a></p>

<p>Here's a screenshot of my front end form:</p>

<p><a href="https://i.sstatic.net/db8QR.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/db8QR.png" alt="enter image description here"></a></p>

<p>I tried adding a hidden input field below the text input field but the problem I ran into is that all placeholder text is submitted regardless of whether the user actually filled out a field.                                                                 </p>

<pre><code>&lt;input type="hidden" name="{{ block.fieldName }}" value="{{ row.label }}"&gt;
</code></pre>

<p>So let's say that the user filled out the "fetal heart rate" text field but did not fill out the "estimated gestational age". Using the hidden text field both "bpm" and "weeks" is submitted even if the user didn't fill out the "estimated gestational age" field.</p>

<p><strong>Summary of my question:</strong> Is there a way to conditionally append a value along user text input? Example, if the user entered "100" for "fetal heart rate" in the front end form text input field, and my placeholder text is "bpm", the result I would like submitted is: "100 bpm". If he skipped filling out any other text input fields then no additional values are submitted (like "weeks").</p>

<form>
Append a placeholder value to a text input field when form is submitted
# Append a placeholder value to a text input field when form is submitted

(Asked by: username_0 on 2016-09-27)

I have a Matrix field in my Craft control panel which contains content which creates a front end form.  If the form field has a placeholder text entered in my Matrix field, I would like that text to be submitted along with the user's text input.
If the user entered "100" in the front end form text input field, and my placeholder text is "bpm", the result I would like submitted is: "100 bpm".
Here's my text input field form code:


Here's a screenshot of my Matrix field:

Here's a screenshot of my front end form:

I tried adding a hidden input field below the text input field but the problem I ran into is that all placeholder text is submitted regardless of whether the user actually filled out a field.                                                                 


So let's say that the user filled out the "fetal heart rate" text field but did not fill out the "estimated gestational age". Using the hidden text field both "bpm" and "weeks" is submitted even if the user didn't fill out the "estimated gestational age" field.
Summary of my question: Is there a way to conditionally append a value along user text input? Example, if the user entered "100" for "fetal heart rate" in the front end form text input field, and my placeholder text is "bpm", the result I would like submitted is: "100 bpm". If he skipped filling out any other text input fields then no additional values are submitted (like "weeks").

**Tags:** form

---

## 1 answers

### Answer by username_1 on 2016-09-27. Score: 1

You could either do this the JavaScript way (listen to onSubmit, grab DOM values, append if necessary, otherwise remove), or the PHP way (from a plugin).

I'll pseudocode the latter because I hate the former.

1. Generate a Craft [plugin](https://pluginfactory.io/).
2. Have it [listen](https://craftcms.com/docs/plugins/hooks-and-events) for the [onBeforeSaveEntry](https://craftcms.com/docs/plugins/events-reference#entries-onBeforeSaveEntry) event.
3. Inside that event listener, you can check if the entry being saved belongs to the section you're interested in.
4. If so, check your Fetal Heart Rate field.
5. If its value is "bpm", set it to null or an empty string.
6. If it's anything other than "bpm", append " bpm" to its existing value.
7. Repeat 4-6 for the "Estimated Gestational Age" field.
8. [Bob's your uncle](https://en.wikipedia.org/wiki/Bob%27s_your_uncle).

> **Comments:**
> **username_0**: Thanks for the steps @Brad.  Creating a plugin looks a bit out of my league so I'll research the javascript option.  

---

