Post an array value, not key, from a dropdown menu

I’m trying to post an array value but instead of posting the value (’’,‘I am a parent’,‘I am a teacher’,‘I am a student’), it appears to post the key instead (0,1,2,3).

The array is used for an dropdown menu.

Please an anyone help?

function add_tml_registration_form_fields() {
	
	tml_add_form_field( 'register', 'customer_type', array(
		'type'     => 'dropdown',
		'label'    => 'Please select whether you are a student, teacher or parent.',
		'value'    => tml_get_request_value( 'customer_type', 'post' ),
		'id'       => 'customer_type',
		'options'  => array('','I am a parent','I am a teacher','I am a student'),
		'priority' => 15,
	) );
}
add_action( 'init', 'add_tml_registration_form_fields' );

function save_tml_registration_form_fields( $user_id ) {
	if ( ! empty( $_POST['customer_type'] ) ) {
		update_user_meta( $user_id, 'customer_type', $_POST['customer_type'] );
	}
}
add_action( 'user_register', 'save_tml_registration_form_fields' );

Well, that shows how it is created. Please show how it is displayed and we can help.
You created an array of options but did not put in the keys. So, I am guessing that the keys are being
displayed in the code you did not show us. The part where it displays the options. Most likely you need to
alter that code, not the above code. Or just try to add in the keys like this:

That’s actually how it should be. When it get’s saved to a database table, it will store that value and it references a different table to get the string value.

Thanks for replying.

The page I’m referring to is here. https://www.revisionbuddies.com/register/

When I look in the profile page after the user has registered a value of 0,1,2 or 3 is displayed.

The code for showing the profile is here…

function tml_edit_user_profile( $profileuser ) {
	?>
	

    <p>
        <label for="customer_type">Please select whether you are a student, teacher or guardian.</label>
        <select name="customer_type" id="customer_type">
            <option selected = true value="<?php echo $profileuser->customer_type; ?>"><?php echo $profileuser->customer_type; ?></option>
            <option value="Parent">I am a parent</option>
            <option value="Student">I am a student</option>
            <option value="Teacher">I am a teacher</option>

        </select>
    </p>
	<?php
}
add_action( 'edit_user_profile', 'tml_edit_user_profile' );

This should be:

<option value=1>I am a parent</option>

And, this way when you save the customer_type, the database table field would be INT not char or text.
Then, when you display it back in the profile, you would have to decode the numbers back into the text.
OR, change the database to just store the text and not use indexes. (You only have the three of them!)
Like:

<option value="I am a parent">I am a parent</option>

Either way works. In the displayed you have to add code to indicate that it is SELECTED if the user is going back to their profile. You would do that like this:

<option value="Parent"<?PHP if ($current_type=1) echo "SELECTED";?>>I am a parent</option>

OR

<option value="Parent"<?PHP if ($current_type="Parent") echo "SELECTED";?>>I am a parent</option>

Depending if you are using numeric or text keys. Hope that helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service