php - WP Ajax not working in else condition -
i working on wordpress plugin. in plugin there check box, when user checked checkbox value saved in database via ajax , when unchecked value deleted database via ajax. create checkbox , write ajax code.
here code:
html code
<?php $cart_items = get_cart_contents(); $cart_info = array(); foreach ($cart_items $_data) { $prod_id = $_data['id']; $cart_info[] = array( 'prod_id' => $prod_id, ); } $_cart_sr = serialize($cart_info); ?> <label class="label" for="purchase"> <?php _e('purchase', 'product'); ?> <input type="checkbox" id="purchase" /> </label> <input type="hidden" class="cart_info" value='<?php echo $_cart_sr; ?>'>
here ajax , php code:
add_action('wp_ajax_values_save', 'save_check_value'); add_action('wp_ajax_nopriv_values_save', 'save_check_value'); add_action('wp_ajax_values_delete', 'delete_check_value'); add_action('wp_ajax_nopriv_values_delete', 'delete_check_value'); add_action("wp_head", "edd_gift_email_ajax"); function edd_gift_email_ajax() { ?> <script type="text/javascript"> jquery(document).ready(function () { jquery("#purchase").click(function () { if(jquery(this).is(":checked")) { var cart_info_save = jquery('.cart_info').val(); var data_save = { action: 'values_save', cart_info_save: cart_info_save } jquery.post('<?php echo admin_url('admin-ajax.php'); ?>', data_save, function (save_result) { alert(save_result); }); } else { var cart_info_delete = jquery('.cart_info').val(); var data_delete = { action: 'values_delete', cart_info_delete: cart_info_delete } jquery.post('<?php echo admin_url('admin-ajax.php'); ?>', data_delete, function (delete_result) { alert(delete_result); }); } }); }); </script> <?php }
and here save , delete query
function save_check_value() { global $wpdb; $info_save = stripcslashes($_request['cart_info_save']); $cart_info_un_sr_save = unserialize($info_save); foreach ($cart_info_un_sr_save $user_gift_cart_save) { $prod_user_id_save = $user_cart_save['prod_id']; echo $prod_user_id_save . " _ add this"; //update_post_meta($prod_user_id_save, 'this_product', '1'); } } function delete_check_value() { global $wpdb; $info_delete = stripcslashes($_request['cart_info_delete']); $cart_info_un_sr_delete = unserialize($info_delete); foreach ($cart_info_un_sr_delete $user_cart_delete) { $prod_user_id_delete = $user_cart_delete['prod_id']; echo $prod_user_id_delete . " _ delete this"; //delete_post_meta($prod_user_id_delete, 'this_product', '1'); } }
so when checked check box alert gives me value 168 _ add this
(this want) when unchecked check box alert gives me value 0
(i want value 168 _ delete this
).
i checked every thing got confused why else condition not give me right value.
any suggestions.
the answer of @ramraider can still reduce more code , might work :)
here's jquery:
<script type="text/javascript"> jquery(document).ready(function() { jquery("#purchase").click( function() { // method variable using define // function should trigger later in our php var cart_method = jquery(this).is(":checked") ? 'save' : 'delete'; var cart_info = jquery('.cart_info').val(); var data = { action: 'modify_cart', cart_method: cart_method, cart_info: cart_info }; jquery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function( result ) { alert( result ); }); } }); }); </script>
and here our php code:
<?php function modify_cart_func() { global $wpdb; // method our jquery $method = $_post['cart_method']; $info = stripcslashes( $_request['cart_info'] ); $cart_info = unserialize( $info ); foreach( $cart_info $gift ) { // if method save if ($method == 'save') { update_post_meta($gift['product_id'], 'this_product', 1); // if method delete } else if ($method == 'delete') { delete_post_meta($gift['product_id'], 'this_product', 1); } } } ?>
and ajax call:
<?php add_action('wp_ajax_modify_cart', 'modify_cart_func'); add_action('wp_ajax_nopriv_modify_cart', 'modify_cart_func'); ?>
hope makes sense ;)
Comments
Post a Comment