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
Add a comment  | 

1 Answer 1

Reset to default 0

Replace 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