Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm customizing the WordPress registration form, and I need to display an list of options from a select on form submission.

I was using the following code to capture the selected option of the select:

<option value="Medicine" id="option-1" <?php if($_POST['name_of_select']=='Medicine') {?> selected="selected" <?Php } ?>>Medicine</option>

Here's a jQuery code to generate the list:

function list(array_list) {

    $("#formOptions").html(""); //reset child options
    $(array_list).each(function (i) { //populate child options
        $("#formOptions").append("<option value=\""+array_list[i].value+"\">"+array_list[i].display+"</option>");
    });

}

And the options of list:

var Option 1 = [
    {display: "Medicine", value: "Medicine" }];

Since the list is loaded according to another option previously selected.

Now I'm using jQuery to generate the list of options select field. But when user send a form, the value of the selected option does not sent. The option is sent blank.

My question is:

How do I capture the selected option and pass in sending the registration form?

I appreciate any tip

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Way use jQery to populate the select field when you can use filter and hook into the form using PHP eg:

 //1. Add a new form element...
add_action('register_form','_register_form_wpa103118');
function _register_form_wpa103118 (){
    $name_of_select = ( isset( $_POST['name_of_select'] ) ) ? $_POST['name_of_select']: '';
    $options = array(
        'Medicine' => 'Medicine',
        'Option2' => 'Option2',
    );
    ?>
    <p>
        <label for="name_of_select"><?php _e('First Name','mydomain') ?><br />
            <select name="name_of_select">
                <?php
                foreach ($options as $key => $value) {
                    $selected = ($value == $name_of_select)? ' selected="selected"': '';
                    echo '<option'.$selected.' value="'.$key.'">'.$value.'</option>';
                }
                ?>
            </select>
    </p>
    <?php
}

//2. Add validation. In this case, we make sure first_name is required.
add_filter('registration_errors', '_registration_errors_wpa103118', 10, 3);
function _registration_errors_wpa103118 ($errors, $sanitized_user_login, $user_email) {
    if ( empty( $_POST['name_of_select'] ) )
        $errors->add( 'name_of_select', __('<strong>ERROR</strong>: You must select ....') );

    return $errors;
}

//3. Finally, save our extra registration user meta. or do whatever you want with it.
add_action('user_register', '_user_register_wpa103118');
function _user_register_wpa103118 ($user_id) {
    if ( isset( $_POST['name_of_select'] ) )
        update_user_meta($user_id, 'name_of_select', $_POST['name_of_select']);
}
share|improve this answer
    
Hi, Thank you Bainternet, is good tip. But, I need to display two select, and the secundary select is populated according the first option selected. For this, I used jQuery. The question is that, I do not get the option selected when the form is submited. –  enoquenroll Jun 16 '13 at 22:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.