0

I have form that i'm using to update user profile from front end and i have country select.

<form method="post" id="adduser" action="<?php the_permalink(); ?>">

    <select id="te" name="usercountry_id">
        <option value="1">Country 1</option>
        <option value="2">Country 2</option>
    </select>

    <p class="form-submit">
    <?php echo $referer; ?>
    <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
    <?php wp_nonce_field( 'update-user' ) ?>
    <input name="action" type="hidden" id="action" value="update-user" />
    </p><!-- .form-submit -->
</form><!-- #adduser -->

When i try to update

$usercountry= $_POST['usercountry_id'];
update_user_meta($id, 'usercountry_id', $usercountry);

my $_POST['usercountry_id'] is empty. Also i tried using jQuery to pass variable to php but with no success:

 jQuery("#te").click(function() {
    var b = jQuery('#te :selected').text();

    var ajaxurl ='<?php echo get_bloginfo("url"); ?>/wp-admin/admin-ajax.php';
       jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"variable": b },

    });});

$_POST['variable'] is again empty.

4
  • 1
    Post the code that does the form processing, please.
    – s_ha_dum
    Commented Mar 17, 2013 at 17:01
  • 2
    You might want to take this question to SO - there it'd be on-topic, anyway. Though, as an aside, $_POST['usercountry_id'] should be populated with either 1 or 2 after form submit. Commented Mar 17, 2013 at 17:01
  • Ok, i found what was the problem. I called $_POST in form itself instead of in form processing code. I was using default wordpress form processing code for user profile. if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'update-user') { if(!empty($_POST['usercountry_id'])) update_user_meta($current_user -> id, 'usercountry_id', esc_attr($_POST['usercountry_id']));} Thank you
    – raaaa
    Commented Mar 17, 2013 at 17:14
  • Please add the last comment as answer and mark it as solution for your question. Commented Feb 11, 2014 at 19:37

2 Answers 2

0

I use something like this, try

<script type="text/javascript">
 document.getElementById('te').value = "<?php echo $_GET['usercountry_id'];?>";
</script>
0

Ok, i found what was the problem. I called $_POST in form itself instead of in form processing code. I was using default WordPress form processing code for user profile.

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) { 
    if ( !empty( $_POST['usercountry_id'] ) )
        update_user_meta($current_user -> id, 'usercountry_id', esc_attr( $_POST['usercountry_id'] ) );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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