Getting the correct value for a post

I’ve added correct indenting to your question, which should make the problem more obvious.

The problem is that you meant:

foreach ( $state as $state )
    update_post_meta($post_id,'state',$state->name);

What you actually put is:

foreach ( $state as $state );
update_post_meta($post_id,'state',$state->name);

Which expands to:

foreach ( $state as $state ) {
    // do nothing
}

//insert post meta
update_post_meta($post_id,'state',$state->name);

A foreach loop will execute what immediatley comes after it. Here an empty statement is what comes after the foreach.

To prevent this happening again:

  • Indent your code correctly
  • Always use braces { } on your for/if/while/do/foreach loops/conditionals

Code editors such as sublime text or Komodo can re-indent code for you quite easily at a click of a button or a command

Here is what your foreach loop should look like:

foreach ( $state as $state ) {
    update_post_meta($post_id,'state',$state->name);
}

edit

I’ve also noticed another mistake that I really should have picked up on before

$state = get_terms('state', 'name');
foreach ( $state as $state ) {
    update_post_meta($post_id,'state',$state->name);
}

notice that you’re using state each time? They shouldn’t be the same. Instead do this:

$states = get_terms('state', 'name');
foreach ( $states as $state ) {
    update_post_meta($post_id,'state',$state->name);
}

foreach state S as state

Also you’re not actually checking if the state is the one that you selected, so something like this:

$states = get_the_terms('state', 'name');
if ( $states && ! is_wp_error( $states ) ){
    foreach ( $states as $state ) {
        if($state->name == $_POST['yourinputsnamehere']){
            update_post_meta($post_id,'state',$state->name);
            break;
        }
}
}