Ajex select change texonomy and post

PHOTO EMBED

Thu Jul 06 2023 11:46:40 GMT+0000 (Coordinated Universal Time)

Saved by @gshailesh27

//Html page code

<li>
<label>Program </label>
<select name="programs"  class="selectedprograms" id="programs">				<option value="">Select Program</option>
													<?php
             $term = get_queried_object();
             $details = get_field("contact_program_list_9_get", $term);
             foreach ($details as $detail) {
                 $term = get_term($detail);
                 echo '<option  value="' .
                     $detail .
                     '"> ' .
                     $term->name .
                     "</option>";
             }
             ?>												</select>
<div id="frm_programs_error" class="errormsg">Please select a program</div>
</li>

//On change ajax code ( add into footer )

$('.selectedprograms').on("change",function(e){
 $('#loadingmessage').show(); 
let id= $(this).val();
		 
var nimi = $(this).val();
    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: "POST",
        data: {
            'action': 'addCustomer',
            'programs': nimi,
        },
        success: function (data) {
            jQuery(".selectedcity").html(data);
			 $('#loadingmessage').hide(); 
        }
    });
});	
		
		



$('.selectedcity').on("change",function(e){
          $('#loadingmessage').show();
			let id= $(this).val();
			let programid=$( "#programs option:selected" ).val();
 			if(programid == "" || programid == undefined){
			   return false;
			}
var nimi = $(this).val();

    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: "POST",
        data: {
            'action': 'post_language',
            'nimi': { code: id, userid: programid },

        },
        success: function (data) {
            jQuery(".selectedregion").html(data);
			$('#loadingmessage').hide(); 
        }
    });
});



//Function for ajax added into function.php

<?php
wp_enqueue_script("jquery");

function addCustomer()
{
    global $wpdb;

    $programs = $_POST["programs"];
    if (!empty($programs)) {
        $args = [
            "post_type" => "contacts",
            "post_status" => "publish",
            "posts_per_page" => -1,
            "tax_query" => [
                [
                    "taxonomy" => "contact_program_type",
                    "field" => "term_id",
                    "terms" => $programs,
                ],
            ],
        ];

        $my_query = new WP_Query($args);
        if ($my_query->have_posts()):
            $cart = [];
            while ($my_query->have_posts()):
                $my_query->the_post();

                $current_id = get_the_ID();
                $term_list = get_the_terms($current_id, "contact_city");
                foreach ($term_list as $term_single) {
                    $cart[] = $term_single->term_id;
                }
            endwhile;
        endif;
        $arrays = array_unique($cart);
        $html = "";
        foreach ($arrays as $array) {
            $html .=
                '<option value="' .
                get_term($array)->term_id .
                '">' .
                get_term($array)->name .
                "</option>";
            // $term_name = get_term( $array )->name;
        }
        echo $html;
    }
}
add_action("wp_ajax_addCustomer", "addCustomer");
add_action("wp_ajax_nopriv_addCustomer", "addCustomer");

function add_our_script()
{
    wp_register_script(
        "ajax-js",
        get_template_directory_uri() . "/scripts/keeled.js",
        ["jquery"],
        "",
        true
    );
    wp_localize_script("ajax-js", "ajax-js_ajax", [
        "ajax_url" => admin_url("admin-ajax.php"),
    ]);
}
add_action("wp_enqueue_scripts", "add_our_script");

function post_language()
{
    $programs = $_POST;

    $args = [
        "post_type" => "contacts",
        "post_status" => "publish",
        "posts_per_page" => -1,
        "tax_query" => [
            "relation" => "AND",
            [
                "taxonomy" => "contact_city",
                "field" => "term_id",
                "terms" => $programs["nimi"]["code"],
            ],
            [
                "taxonomy" => "contact_program_type",
                "field" => "term_id",
                "terms" => $programs["nimi"]["userid"],
            ],
        ],
    ];

    $my_query = new WP_Query($args);
    if ($my_query->have_posts()):
        $cart = [];
        while ($my_query->have_posts()):
            $my_query->the_post();

            $current_id = get_the_ID();
            $term_list = get_the_terms($current_id, "contact_city");
            foreach ($term_list as $term_single) {
                $cart[] = $current_id;
            }
        endwhile;
    endif;

    $arrays = array_unique($cart);
    $html = "";
    foreach ($arrays as $array) {
        $html .=
            '<option value="' .
            $array .
            '">' .
            get_the_title($array) .
            "</option>";
    }

    echo $html;
}
add_action("wp_ajax_post_language", "post_language");
add_action("wp_ajax_nopriv_post_language", "post_language");
?>


content_copyCOPY