0

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 );               
                }
            }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

you need to echo $url if it's in the javascript.
<?= is shorthand to echo.
yeah, <?=$url?> would do it.
so where i can i add my php variable in ajax? here is my variable $erf_header->purpose = "RMA"
sorry i cannot get what you mean in this code im just new in ajax
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.