So i have 4 php variables named $description,$hscode,$from,$to
I wants to send these variables through a AJAX request.My incomplete attempt is like this:
function searchFilter(page_num) {
page_num = page_num?page_num:0;
$.ajax({
type: 'POST',
url: 'getData.php',
data: { page : +page_num, "desc" : "<?php print $description ?>","hscode" : "<?php print $hscode ?>","from" : "<?php print $from ?>","to" : "<?php $to ?>" },
beforeSend: function () {
$('.container-fluid').waitMe({
effect : 'stretch',
bg : 'rgba(255,255,255,0.7)'
});
},
success: function (html) {
$('#posts_content').html(html);
$('.container-fluid').waitMe("hide");
}
});
}
The problem is, PHP variables are dynamic means they are not always set sometime only $description
is set, sometime only $hscode
is set ,sometime $description
and $hscode
only set,There are 6 combination of this type.Please help in this situation how can i send this request.
POSSIBLE Answer:
code is working fine but i am confused is it a right way to do it,Please correct me if i am wrong,my working code is :
function searchFilter(page_num) {
page_num = page_num?page_num:0;
$.ajax({
type: 'POST',
url: 'getData.php',
data : { page : +page_num,
<?php if(isset($description)){ echo '"desc"'.' : '. "'$description'" . ',';} ?>
<?php if(isset($hscode)){ echo '"hscode"'.' : '. "'$hscode'" . ',';} ?>
<?php if(isset($from)){ echo '"from"'.' : '. "'$from'" . ',';} ?>
<?php if(isset($to)){ echo '"to"'.' : '. "'$to'" . ',';} ?> },
beforeSend: function () {
$('.container-fluid').waitMe({
effect : 'stretch',
bg : 'rgba(255,255,255,0.7)'
});
},
success: function (html) {
$('#posts_content').html(html);
$('.container-fluid').waitMe("hide");
}
});
}