Reputation: 538
I have a variable in php and i want to check in ajax if this variable value is word "RMA" or not.
If the variable value is "RMA" then my url ajax will change to
url: "<?=site_url('equip_request/get_json_selected');?>",
else
url: "<?=site_url('spares/get_json_selected');?>",
Code:
$erf_header->purpose = "RMA"; // this is just an example variable and value
and this is my function in ajax
if ($.cookie("spare-items-loaded") == 1) {
if ($.cookie("spare-items")) {
cookie_items = $.cookie("spare-items").split(",");
if (cookie_items.length > 0) {
var request = $.ajax({
url: "<?=site_url('spares/get_json_selected');?>",
type: "POST",
data: {
ids: cookie_items.join(","),
},
dataType: "json",
success: function(data) {
var template = null;
var source = null;
var result = null;
result = jQuery.parseJSON(JSON.stringify(data));
source = $("#spare-loop-list").html();
template = Handlebars.compile(source);
$("#equipment-list").append(template(result));
cookie_items = jQuery.unique(cookie_items);
$("#total-spares").html(cookie_items.length);
}
});
}
} else {
$("#equipment-container").hide();
$("#total-spares").html( 0 );
}
}
Upvotes: 0
Views: 321
Reputation: 1941
First we decide which URL we'll be sending POST
data to.
$url = ''; //Initialize $url as global variable
if($var === "RMA"){
$url = "site_url('equip_request/get_json_selected')";
}else{
$url = "site_url('spares/get_json_selected')";
}
Then parse in the $url into the ajax request.
url: "<?=$url;?>",
On a sidenote, i'd be weary when using <?=
, here is why.
Additionally, by default, JS can only parse PHP if it's inline of a .php
file, if it's not, see your options here.
Upvotes: 1