How to fill in email field automatically when the user selects an option from the dropdown field


How to fill in email field automatically when the user selects an option from the dropdown field.

I have added this code to functions.php. But this code can only display the post title. I also want to display the email from the post content to email field but only when user select an option from dropdown.

So let say if the user select gravity on dropdown list, in email field will display

add_filter(“gform_pre_render_6”, agent_commission);
function agent_commission($form2){

//Reading posts for "Business" category;
$posts = get_posts("category=School");
$content = get_the_content();

//Creating drop down item array.
$school = array();

//Adding initial blank value.
$school[] = array("text" => "", "value" => "");

//Adding post titles to the items array
foreach($posts as $post1)
    $school[] = array("value" => $post1->post_title, "text" => $post1->post_title);

//Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form2["fields"] as &$field)
    if($field["id"] == 19){
        $field["type"] = "select";
        $field["choices"] = $school;

return $form2;


Do you specifically need to show the email address in the form, or are you trying to use that email address later, in the notification or notification routing?

If you want it to display the email address in the drop down, you will need to modify this line:

$school[] = array("value" => $post1->post_title, "text" => $post1->post_title);

That line is setting both the display text and the submitted value to “post_title”. You will need to retrieve the value from the post, and use that for the ‘text’ property here. I’m not sure how you are storing the email address, but if you can show or explain how the email is stored and associated with the post, we can help you retrieve that email address and set the drop down display text.

Hi Chris. Thanks for your feedback.

So in my case, let’s say i have 2 form. One for school information that contains school name and it’s email address, and the other form is used for create invoice. When create invoice, client’s name, title and client’s email is required. So i need some data such as school name and it’s email address from school information for field school name (dropdown field) and email address (email field) in invoice form. So, when user create invoice and selects the school name then the email will auto filled according to user selection.