پیکسلر | شبکه اجتماعی برنامه نویسان

چطور یک آرایه را از ajax در jquery به اسکریپت php منتقل کنم؟

M-R 3 سال پیش

من یک تابع ajax دارم که به یک فایل php متصل شده. دارم سعی می کنم متغییری با نام “tax_query” رو از صفحه بگیرم و اطلاعات رو از طریق ajax به فایل php که مسئول به کارگیری این متغییرها است، منتقل کنم. 

فعلا متغییرها رو به صورت دستی در فایل php وارد کردم و به درستی کار میکنه. اما من می خوام به جای این متغییرها که به صورت دستی وارد کردم، اطلاعات به صورت خودکار و از طریق کاربر تغییر کنند.

کد زیر، کد jquery منه که اطلاعات رو از کاربر میگیره، اون رو ذخیره می کنه و از طریق ajax به php منتقل میکنه:

(function($) {

function getFilters () {
var filters = {}

$('.checked').each(function () {
filters[this.dataset.filter] = this.dataset.value
})

return filters
}

$(".dropdown-menu li a").click(function (event) {
var $this = $(this)
var $dropdown = $this.parents('.dropdown')
var html = $this.text() + ' <span class="caret"></span>'

$dropdown.find(".btn").html(html);
$dropdown.find('a').removeClass('checked');
$this.addClass('checked');

var filters = getFilters();

//var level = filters["level"];
var level = filters.level;
var location = filters.location;
var specialty = filters.specialty;

alert("Level: "+level+" Location: "+location+" Specialty: "+specialty);

console.log(getFilters());

event.preventDefault();

alert("made it up to ajax");

$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php');?>',
dataType: "html", // add data type
data: { action : 'get_ajax_posts' },
success: function( response ) {
console.log( response );

$( '.posts-area' ).html( response );
}
});
})

})(jQuery);

این هم تابع php منه که یک کوئری وردپرس رو اجرا میکنه و به jquery برمیگردونه:

function get_ajax_posts() {
// Query Arguments
$args = array(
'post_type' => 'the-talent',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'level',
'field' => 'term_id',
'terms' => array( '25' ),// needs to be 3 and up? maybe use if statements?
),
array(
'taxonomy' => 'location',
'field' => 'term_id',
'terms' => array( '20' ),
),
array(
'taxonomy' => 'specialty',
'field' => 'term_id',
'terms' => array( '4' ),
),

),
);

// The Query
$ajaxposts = new WP_Query( $args );

$response = '';

// The Query
if ( $ajaxposts->have_posts() ) {
while ( $ajaxposts->have_posts() ) {
$ajaxposts->the_post();
//$response .= get_template_part('products');

$response .=

$name = get_field('name');
$main_image = get_field('main_image');

?>

<div class="col-sm-6 col-md-3 talent">
<div class="talent">
<a type="button" href="<?php the_permalink() ?>">
<img class="img-responsive" src="<?php echo $main_image; ?>">
<h3 class="dark"><?php echo $name; ?></h3>
</a>
</div><!-- close talent -->
</div><!-- close col -->

<?php

;
}
} else {
$response .= get_template_part('none');
}

//echo $response;

exit; // leave ajax call
}

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts');

 

1 پاسخ
برای قرار دادن دیدگاه وارد شوید
Mostafa 3 سال پیش

تقریبا تمام کارها رو انجام دادی. متغییر filters رو به data که در قسمت ajax نوشتی، اضافه کن:

$.ajax({
//..
data:{action: 'get_ajax_posts' , filters:filters}
})

این متغییر در اسکریپت php در دسترسه. برای این کار به صورت زیر عمل کن

$query = ['relation' => 'AND'];

foreach ($_POST['filters'] as $key => $value){
$query[] = [
'field' => 'term_id',
'taxononomy' =>$key,
'terms' => [$value]
];
}