I can not update the meta from this ajax. Can someone give me a direction to help me to resolve it.
I have a select field. I need to grab his value, make some work with that value and then, add it to an input field "union_id_field". The form is actually correctly working as the jquery grab the value, the ajax do the work and the jquery add the last value to the input.
The problem is the value is not saved as meta and it return nothing when form is submitted. What is the problem with the meta ?
It's a part of WP user frontend pro form
Here's the function that add the field to the existing form :
/**
* Add the input field to the form
*
* @param int $form_id
* @param null|int $post_id
* @param array $form_settings
*/
function render_grab_union_id( $form_id, $post_id, $form_settings ) {
$value = '';
if ( $post_id ) {
$value = get_post_meta( $post_id, 'union_id_meta', true );
}
?>
<div class="wpuf-label">
<label>Notification to</label>
</div>
<div class="wpuf-fields">
<input type="text" id="union_id_field" name="union_id_field" value="<?php echo esc_attr( $value ); ?>" disabled>
</div>
<script type="text/javascript">
var $email = jQuery.noConflict();
$email(document).ready(function(){
$email("#union\\[\\]").change(function() {
var union_id = $email(this).val();
$email.ajax({
type: "POST",
url: "http://www.mysite.net/wp-admin/admin-ajax.php", // check the exact URL for your situation
dataType: 'html',
data: {
action: 'referent_email',
union_id: union_id
},
success: function(data){
$email("#union_id_field").val(data);
},
error: function(data)
{
alert("Your browser broke!");
return false;
}
});
});
});
</script>
<?php
}
add_action( 'grab_union_id', 'render_grab_union_id', 10, 3 );
/**
* Update the custom field when the form submits
*
* @param type $post_id
*/
function update_grab_union_id( $post_id ) {
if ( isset( $_POST['union_id_field'] ) ) {
update_post_meta( $post_id, 'union_id_meta', $_POST['union_id_field'] );
}
}
add_action( 'wpuf_add_post_after_insert', 'update_grab_union_id' );
add_action( 'wpuf_edit_post_after_update', 'update_grab_union_id' );
And the ajax :
function referent_email() {
//get the union ID
$union_id= $_POST['union_id'];
// $opts = get binduser_term array
$opts = get_option("myoptions_terms");
foreach($opts as $key => $value) {
//If $union_id is in $opts
if (in_array($union_id, $value)) {
//Get the main key of this value witch is also the user ID in $opts
$first_key = $key;
// Get userdata based on main key user ID in $opts
$user_info = get_userdata($first_key);
$user_email = $user_info->user_email;
print_r($user_email);
}
}
die();
}
add_action('wp_ajax_referent_email', 'referent_email');
add_action('wp_ajax_nopriv_referent_email', 'referent_email');
What I need is to add the $user_email value as a "union_id_field" post_meta