Support

Account

Home Forums Front-end Issues Use data from ACF field to automatically populate another form field

Solving

Use data from ACF field to automatically populate another form field

  • Hi everyone,
    I’m new to both ACF and WordPress, so excuse me if the question is unappropriate. I have the following situation that I don’t know how to handle.
    I have a frontend form to register users. While registering, users should define the company they work for. If the company is not already present in the database, they should add it. The company is a custom type and I handle it using ACF, which is called in a modal in the form page.
    The form depicted in the modal is made using ACF, as follows, in my add-company-form.php:

    acf_form_head();
    
    get_header();
    
    <div id="add-company-dialog" class="pr-add-company">
        <h1 class="pr-dialog-title">Add new company/institute</h1>
        <?php
    	
    	acf_form(array(
    		'post_id'		=> 'new_post',
    		'post_title'	=> true,
    		'post_content'	=> false,
    		'new_post'		=> array(
    			'post_type'		=> 'company',
    			'post_status'	=> 'publish'
    		),
    		'submit_value'	=> 'Create Company'
    	));
    	?>
    </div>

    While the main form is in my signup-form.php and is the following:

    <div class="pr-signup-page">
    
        <form class="pr-signup-form" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="POST">
            <fieldset class="pr-signup-form__fieldset">
            <div class="pr-signup-form__field-wrap-one-trd">
                <label class="pr-signup-form__field-label" for="pr-name"><?php echo __("First name", N7_PRIMIS_TEXTDOMAIN)?>*</label>
                <input type="text" name="pr-name" required>
            </div>
            <div class="pr-signup-form__field-wrap-one-trd-last">
                <label class="pr-signup-form__field-label" for="pr-last-name"><?php echo __("Last name", N7_PRIMIS_TEXTDOMAIN)?>*</label>
                <input type="text" name="pr-last-name" required > 
            </div>
            <div class="pr-signup-form__field-wrap-one-trd">
                <label class="pr-signup-form__field-label" for="pr-inst-email"><?php echo __("Institutional email", N7_PRIMIS_TEXTDOMAIN)?>*</label>
                <input type="email" name="pr-inst-email" required>
                <span class="pr-signup-form__field-help"><?php echo __("Insert your official email account of your insitution/company", N7_PRIMIS_TEXTDOMAIN)?></span>
            </div>
            </fieldset>
            <fieldset class="pr-signup-form__fieldset">
            <div class="pr-signup-form__field-wrap-one-trd pr-signup-form__institute">
                <label class="pr-signup-form__field-label" for="pr-company"><?php echo __("Company / Institute", N7_PRIMIS_TEXTDOMAIN)?>*</label>
                <input type="hidden" name="pr-company-id" id="autocomplete-company-id">
                <input type="text" name="pr-company" id="autocomplete-company" required autocomplete="on">
            </div>
            <div class="pr-signup-form__field-wrap-one-trd-last">
                <label class="pr-signup-form__field-label" for="pr-department"><?php echo __("Department", N7_PRIMIS_TEXTDOMAIN)?></label>
                <input type="text" name="pr-department">
            </div>
            <div class="pr-signup-form__field-wrap-one-trd">
                <label class="pr-signup-form__field-label" for="pr-inst-email"><?php echo __("FAX", N7_PRIMIS_TEXTDOMAIN)?></label>
                <input type="text" name="pr-fax">
            </div>
            <div class="pr-signup-form__field-wrap-one-trd">
                <label class="pr-signup-form__field-label" for="pr-inst-email"><?php echo __("Phone", N7_PRIMIS_TEXTDOMAIN)?></label>
                <input type="tel" name="pr-tel">
            </div>
            <div class="pr-signup-form__field-wrap-one-trd">
                <label class="pr-signup-form__field-label" for="pr-inst-email"><?php echo __("Mobile Phone", N7_PRIMIS_TEXTDOMAIN)?></label>
                <input type="tel" name="pr-mobile">
            </div>   	
            <div class="pr-signup-form__field-wrap field-checkbox">
                <input type="checkbox" name="pr-condition-accepted" required value="1">
                <label class="pr-signup-form__field-label" for="pr-subscribe-nws"><?php echo sprintf( __( 'I have read the information about the <a target="_blank" href="%s">Privacy Policy</a> and I agree the use of my personal data.', 'zaki' ), get_permalink( icl_object_id( 12205, 'page' ) ) ); ?></label>
            </div>
        
    
      
            <div class="pr-signup-form__field-action-wrap  pr-signup-form__field-action-wrap-centered">
            <input type="hidden" name="action" value="pr_register_certified"> 
                <button type="submit" class="pr-btn pr-btn-green pr-signup-form__submit" value="<?php echo __("Sign Up as Certified User", N7_PRIMIS_TEXTDOMAIN) ?>">
               
                <i class="fas fa-badge-check"></i> 
                    <?php echo __("Sign Up as Certified User", N7_PRIMIS_TEXTDOMAIN) ?>
                </button>
            </div>   
    
            
            
            </fieldset>        
        </form>
    </div>
    
    <?php pr_add_partial("public/partials/n7-primis-add-company-form", array(), true); ?>

    So here is my question. Is it possible, when a user fills in the modal (hence adds a new company) to automatically populate the company field in the main form with the name of the newly added company in the ACF form?
    Any suggestion or hint would be really appreciated since I really am a beginner here.
    Many thanks,
    Giulia

  • Hi Giulia, did you ever figure this out?

    Cheers, Brian

  • I cannot say exactly how you would do this. You would need to somehow return the post ID created to the page, possibly by setting a cookie or some other means when the post is created. You could then fire an action in JS when the model is closed to get that value and populate your other form.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Use data from ACF field to automatically populate another form field’ is closed to new replies.