admin管理员组文章数量:1122826
The form is in post creating window. Ajax is posting values, but the values doenst renews. Where can be problem ?
Ajax
<script>
function dynamic_Select(field, aid, value)
{
console.log(field, aid,value);
jQuery.ajax({
type: "POST",
url: ajaxurl,
data:{
field: field,
aid: aid,
value: value,
},
error: function(){alert('Error!')},
success: function(){alert(value)}
});
}
</script>
functions.php
add_action('wp_ajax_dynselect', 'update_dynamic_select');
add_filter("attachment_fields_to_save", " update_dynamic_select", null , 2);
function update_dynamic_select($field, $aid, $value) {
update_post_meta($aid, $field, $value);
}
HTML:
<select aid="104" name="_image_matmenys" onchange="dynamic_Select(this.getAttribute('name'), this.getAttribute('aid') ,this.value)">
<option value="10x10">10x10</option>
<option value="20x20" selected="selected">20x20</option>
<option value="30x30">30x30</option>
</select>
The form is in post creating window. Ajax is posting values, but the values doenst renews. Where can be problem ?
Ajax
<script>
function dynamic_Select(field, aid, value)
{
console.log(field, aid,value);
jQuery.ajax({
type: "POST",
url: ajaxurl,
data:{
field: field,
aid: aid,
value: value,
},
error: function(){alert('Error!')},
success: function(){alert(value)}
});
}
</script>
functions.php
add_action('wp_ajax_dynselect', 'update_dynamic_select');
add_filter("attachment_fields_to_save", " update_dynamic_select", null , 2);
function update_dynamic_select($field, $aid, $value) {
update_post_meta($aid, $field, $value);
}
HTML:
<select aid="104" name="_image_matmenys" onchange="dynamic_Select(this.getAttribute('name'), this.getAttribute('aid') ,this.value)">
<option value="10x10">10x10</option>
<option value="20x20" selected="selected">20x20</option>
<option value="30x30">30x30</option>
</select>
Share
Improve this question
asked Sep 10, 2014 at 17:07
MindauMindau
1131 silver badge6 bronze badges
2
- Does your ajax request alert proper values on change or not? 'Ajax is posting values, but the values doesn't renews', meaning what? Does that mean, alert shows proper values but dropdown doesnt show selected value? – Domain Commented Sep 10, 2014 at 17:48
- Yes it alert proper values $aid is attachment id, $value is value and $field is the name of custom field.Ajax is posting values, but the values doesn't renews', means that all fields are being posted properly, but my function does not update fields, after reflesh old values comes – Mindau Commented Sep 10, 2014 at 17:56
1 Answer
Reset to default 0Replace the function into your functions.php file -
function update_dynamic_select() {
update_post_meta($_POST['aid'], $_POST['field'], $_POST['value']);
echo $_POST['value'];
die();
}
Replace options in your HTML part -
<?php
$val = get_post_meta( 104, '_image_matmenys', true );
$s1 = ($val == "10x10") ? 'selected="selected"' : '';
$s2 = ($val == "20x20") ? 'selected="selected"' : '';
$s3 = ($val == "30x30") ? 'selected="selected"' : '';
?>
<option value="10x10" <?php echo $s1;?>>10x10</option>
<option value="20x20" <?php echo $s2;?>>20x20</option>
<option value="30x30" <?php echo $s3;?>>30x30</option>
EDIT:
Also, add one more hook -
add_action( 'wp_ajax_nopriv_dynselect', 'update_dynamic_select' );
and make sure that variable ajaxurl
has value admin_url('admin-ajax.php')
i.e. var ajaxurl = '<?php echo admin_url("admin-ajax.php");?>'
本文标签: filterschange attachment custom field onChange event
版权声明:本文标题:filters - change attachment custom field onChange event 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283859a1927107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论