Sunday, June 26, 2022
HomeWordPress Developmentjquery - Ajax Favourite from foreach (tips on how to specify which...

jquery – Ajax Favourite from foreach (tips on how to specify which consequence result’s processed)


I created my first ajax script which is an add-to-favorites choice on my actual property web site. On the only property web page the setup works nice…I verify if the property is already saved, show a stuffed in coronary heart if sure or a coronary heart define if not, then if the guts is clicked it toggles and provides a brand new entry into the consumer meta fields with out reloading the web page.

I then created a favourite’s web page which makes use of a foreach loop to show every saved property. I can verify if the itemizing exists and show the right coronary heart icon however I am not sure tips on how to inform the jQuery code which foreach consequence to get variables from. If I click on on the icon it will toggle however nothing is added or faraway from the database.

My code:

HTML:

<?php 
$user_id = get_current_user_id();
$parameters = array (
    'URL' => http://web site.com/property/3,
    'Address1' => 123 Most important St,
    'Address2' => Roselle, IL 60172,
);
$testpara = $parameters['URL'];
$string = serialize($parameters);

$meta = get_user_meta($user_id, 'pluginlink'); 
if(array_search($testpara, array_column($meta, 'URL')) !== false) {
    $icon = "fa fa-heart";
} else {
    $icon = "fa fa-heart-o";
    }
?> 
<type>
<div class = "buttons"><i onClick="myFunction(this)" class="<?php echo $icon; ?>" id="name" worth="name"></i> </div>
 </type>

jQuery:

<script>       
jQuery(doc).prepared(perform($) {
            var user_id = '<?php echo $user_id; ?>';
            var parameters="<?php echo $string; ?>";
            var testpara="<?php echo $testpara; ?>";
            $('#name').click on(perform(e) {
                    e.preventDefault(); // forestall type from reloading web page
    
            $.ajax({
                url: '/wp-admin/admin-ajax.php',
                knowledge: {
                    'motion': 'save_listing',
                    'user_id': user_id,
                    'parameters': parameters,
                    'testpara': testpara,
                    },
                }); 
                });
            });
    perform myFunction(x) {
      x.classList.toggle("fa-heart-o");
      x.classList.toggle("fa-heart");
    }
</script>

capabilities.php

perform save_user_listing() {
if(!userid) {
    die();
} else {
    if(isset($_REQUEST)){
        $userid = $_REQUEST['user_id'];
        $testparameter = $_REQUEST['testpara'];
        $para = $_REQUEST['parameters'];        
        $unser = unserialize(stripslashes($para));
    
        $meta = get_user_meta($userid, 'pluginlink'); 
        if(array_search($testparameter, array_column($meta, 'URL')) !== false) {
            delete_user_meta($userid, 'pluginlink', $unser);
            } else {
            add_user_meta($userid, 'pluginlink', $unser);
            };
        }//finish if isset
}//shut if userID
        die();
    }//finish perform
add_action('wp_ajax_save_listing', 'save_user_listing');

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments