• Resolved mtrebus1

    (@mtrebus1)


    Hej! How can i get data from my external sql database to set it into the select field of my forminator form. I need to create a field to select from countries. The address field is not working for me, because i need the countries from my database. If you need further information, just let me know. Thanks for your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello @mtrebus1 !

    I trust you’re doing well today!

    If that’s only about getting a list of countries, you can instead use a Select field and enable the Bulk Edit option instead of defining options manually and the predefined list of countries that’s available there. That list is editable, so you can adjust it to your needs.

    Please let us know in case you’d actually need something more complex than that.

    Kind regards,
    Pawel

    Thread Starter mtrebus1

    (@mtrebus1)

    Hello Pawel, thanks for your reply. Unfortunatly i need more than a countries list. This was one case. I will need a select field in forminator that gets his data from a external sql database. This database will be updated too. The select field has to show the updated data too. Thanks for your help,

    Kind regards,

    Matthias

    Thread Starter mtrebus1

    (@mtrebus1)

    Hej Pawel, i found this snippet https://gist.github.com/wpmudev-sls/fd71b5528ed894ebd5b42f5afbcf61e7 Could this work when there is a db connection that populates the values in line 30? Thanks in advance, Matthias

    Hello @mtrebus1 !

    Yes, I was about to share this one. You’ll need to replace that array with code that grabs the data from the external database. I’d suggest to store it in the database along with a timestamp to cache it and avoid making a request each time – this may slow your loading times otherwise.

    You can also try this snippet:

    add_filter( 'forminator_field_single_markup', function( $html, $id, $required, $options ){
    
    	$select_id = 'select-1-field';
    
    	if ( $select_id != $id ) {
    		return $html;
    	}
    
    	$doc = new DOMDocument;
    	$doc->loadHTML( $html );
    	$field = $doc->getElementById( $select_id );
    
    	// The custom fields. Could be imported form a csv file.
    	$options = array(
    		'value_one'   => 'Content one',
    		'value_two'   => 'Content two',
    		'value_three' => 'Content three',
    	);
    	
    	while ( $field->hasChildNodes() ) {
    		$field->removeChild( $field->firstChild );
    	}
    
    	if ( ! empty( $options ) ) {
    		foreach ( $options as $value => $content ) {
    			$option = $doc->createElement( 'option', $content );
    			$option->setAttribute( 'value',$value );
    			$field->appendChild( $option );
    		}
    	}
    
    	return $doc->saveHTML();
    
    }, 20, 4 );

    Similar to the snippet you shared, the $options array should be requested from the external database.

    Kind regards,
    Pawel

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @mtrebus1 ,

    We haven’t heard from you for several days now, so it looks like you don’t have more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

    Thread Starter mtrebus1

    (@mtrebus1)

    Hej, unfortunatly i′m busy with another task now, but will let you know asap, if this is working…

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How can i get data from my external sql database into the select field’ is closed to new replies.