我无法从这个ajax更新meta。有人能给我一个方向来帮助我解决这个问题吗。
我有一个选择字段。我需要获取他的值,对该值进行一些处理,然后将其添加到输入字段“union\\u id\\u field”。当jquery获取值,ajax完成工作,jquery向输入中添加最后一个值时,表单实际上工作正常。
问题是该值没有保存为meta,并且在提交表单时不会返回任何内容。meta有什么问题?
这是WP user frontend pro表单的一部分
以下是将字段添加到现有表单的函数:
/**
* 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\' );
还有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\');
我需要的是将$user\\u email值添加为“union\\u id\\u field”post\\u meta