how to save new value of autocomplete field with hook_form_submit? creating a custom submit function for the form of mymodule

/**
 * Implements hook_form_submit()
 */
function mymodule_form_submit($form, &amp;$form_state){

    $new_user = drupal_anonymous_user();
    // removed basic user fields here to make it more clear

    // save taxonomy checkbox field
    foreach ($form_state['values']['third']['field_functioncat'] as $key =&gt; $value) {
        if($value &gt; 0){
            $new_user-&gt;field_functioncat[LANGUAGE_NONE][$key]['tid'] = $value;
        }
    }

    // save text field
    $new_user-&gt;field_firstname[LANGUAGE_NONE][0]['value'] = $form_state['values']['first']['field_firstname'];

    user_save($new_user); 
}


Above code works but I'm having trouble saving the autocomplete field.

What I'm trying:

// get value of the form
$tid = taxonomy_get_term_by_name($form_state['values']['first']['field_city'], 'field_city');
// get the first array value and then the tid of the left over object:
$tid = reset($tid)-&gt;tid;
// save field to user profile
$new_user-&gt;field_city[LANGUAGE_NONE][$tid]['tid'] = $tid;


This works for existing terms, but when I put in a new term it can't get the id of the term because that term name is not created?

How do I create the term name and get the tid to submit it to the user creation?

note:
I've created the field_city as taxonomy term and linked it to the user profile so it's visible and editable in on the user edit profile page.
As you said if you're unable to find the tid then its a new term. You can save the new term using taxonomy_term_save()

For example,

$term = new stdClass();
$term-&gt;vid = &lt;field city vid&gt;;
$term-&gt;name = $form_state['values']['first']['field_city'];
taxonomy_term_save($term);


After saving the new term you can access the tid like this,

$term-&gt;tid


If you want to fetch the vid referenced with particular field then look at the following thread - How to get the field settings in drupal 7