user3089184
user3089184

Reputation: 241

Include PHP variable in Contact Form 7 field

I have used Contact Form 7 to a create contact form for a WordPress website. One of the fields is called Subject and has an ID of #subject_field

In my PHP file I have a variable $reference which generates a unique code which I would like to be the placeholder text for my subject field.

How can I implement this variable into my contact form?

I have tried using this method of placing this code in my php file but am having no luck.

<script type="text/javascript">
jQuery(document).ready(function($) {
 <?php   
  $('subject_field').val('$reference');
 ?>
});
</script>

Upvotes: 2

Views: 9978

Answers (1)

MrHunter
MrHunter

Reputation: 1900

This should insert the value as long as $reference is defined and this js is included on the same page as the form.

<script type="text/javascript">
jQuery(document).ready(function($) {
  $('#subject_field').val('<?php echo $reference; ?>');
});
</script>

Upvotes: 8

Related Questions