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 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.

share|improve this question
1  
Post the code that does the form processing, please. –  s_ha_dum Mar 17 '13 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. –  Johannes Pille Mar 17 '13 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 Mar 17 '13 at 17:14
    
Please add the last comment as answer and mark it as solution for your question. –  ialocin Feb 11 at 19:37

2 Answers 2

I use something like this, try

<script type="text/javascript">
 document.getElementById('te').value = "<?php echo $_GET['usercountry_id'];?>";
</script>
share|improve this answer
up vote 0 down vote accepted

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'] ) );
}
share|improve this answer

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.