user3089184
user3089184

Reputation: 241

PHP Setting a value of a form field based on id

My WordPress contact form is generated like this by duplicating this line until the form is complete. The form has many fields which are determined in the WP dashboard.

 <input type="text" name="<?php echo "umbheadfld_".$field["field_name"]; ?>" id="<?php echo "umbheadfld_".$field["field_name"]; ?>" data-required="<?php echo $field["required"]; ?>" data-fieldtype="<?php echo $field["field_type"]; ?>">

However, the theme does not allow an option to set a value for the fields. I am trying to set my value as the name of the WordPress post.

Using PHP I can obtain the name of the WordPress post using this code

$title = get_the_title‎();

The first in this post code generates a field defined as id="umbheadfld_Subject" as I named the field "Subject" in the WP dashboard.

I need to ammend the above code to include the value = $title if id=umbheadfld_Subject.

Any ideas?

Upvotes: 0

Views: 681

Answers (1)

Whitebird
Whitebird

Reputation: 185

You can add a single line if statement using terniary operators.

<?php echo ($field["field_name"] == "Subject" ? "value=\"" .$title."\"" : "") ?>

When added to your first line:

 <input type="text" <?php echo ($field["field_name"] == "Subject" ? "value=\"" .$title."\"" : "") ?> name="<?php echo "umbheadfld_".$field["field_name"]; ?>" id="<?php echo "umbheadfld_".$field["field_name"]; ?>" data-required="<?php echo $field["required"]; ?>" data-fieldtype="<?php echo $field["field_type"]; ?>">

Upvotes: 0

Related Questions