BuddyDev

Search

How to create a dropdown select box

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #19251

    Hello,

    Please how can I create a dropdown select box using the code below

     /*
     * Add the Bank details field to the affiliate registration form
     */
    function wp_add_recipient_bank_field_to_affiliate_registration_form() {
    	$errors = affiliate_wp()->register->get_errors();
    	if ( ! array_key_exists( 'empty_recipient_bank', $errors ) ) {
    		$recipient_bank = sanitize_text_field( $_POST['affwp_recipient_bank'] );
    	}
    	?>
    	<p>
    		<label for="affwp-recipient-bank">Recipient Bank (e.g First Bank, GT Bank etc)</label>
    		<input id="affwp-recipient-bank" type="text" name="affwp_recipient_bank" value="<?php if ( ! empty( $recipient_bank ) ) {
    			echo $recipient_bank;
    		} ?>" title="Recipient Bank" />
    	</p>
    	<?php
    } 

    I want to list the following banks in a single dropdown select box
    1. First bank
    2. Gt bank
    3. Zenith bank

    Thank you very much

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #19271

    Hi Tosin,
    The code needs more context.
    Something like this may work.

    
    
    /*
     * Add the Bank details field to the affiliate registration form
     */
    function wp_add_recipient_bank_field_to_affiliate_registration_form() {
    	$errors = affiliate_wp()->register->get_errors();
    	$recipient_bank = '';
    	if ( ! array_key_exists( 'empty_recipient_bank', $errors ) ) {
    		$recipient_bank = sanitize_text_field( $_POST['affwp_recipient_bank'] );
    	}
    
    	$options = array(
    	        'firs_bak' => 'First Bank',
                'gt_bank'   => 'GT Baak',
                'zenith_bank' => 'Zenith bank',
        );
    	?>
        <p>
            <label for="affwp-recipient-bank">Recipient Bank (e.g First Bank, GT Bank etc)</label>
            <select id="affwp-recipient-bank" name="affwp_recipient_bank">
                <?php foreach ( $options as $key => $label ):?>
                    <option value="<?php echo esc_attr( $key );?>" <?php selected($recipient_bank, $key ) ?>><?php echo esc_html( $label );?></option>
                <?php endforeach;?>
            </select>
        </p>
    	<?php
    }
    

    You should test and confirm it and if it does not work, check with affilate wp as it is related to them.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved