0

I am trying to pass the var value from the function to the AJAX URL

function gonative_onesignal_info(info) {
    //var pushid = info.installationId;
    //var subscribed = info.oneSignalSubscribed;

    // EXAMPLE
    var subscribed = true;
    var pushid = "123456789";
}

if (subscribed === true) {
    $.ajax({ url: "ajax.php?push=" + pushid + "&key=<? echo $userkey; ?>",
        context: document.body,
        success: function(){
        }});
}

only the php value is working for me but so in the console log it display like this: ajax.php?push=" + pushid + " instead of ajax.php?push=123456789

Pritesh
  • 1,066
  • 3
  • 15
  • 35
DigiNet Events
  • 357
  • 2
  • 14
  • Why are you calling the $.ajax outside of your function ? Is there any particular reason ? – Quentin Klein Aug 12 '19 at 07:54
  • Either you can define `pushid` as (Global Variable Declaration)[https://stackoverflow.com/a/5786899/6656706] or put you `ajax` code into function. – Jaydeep Mor Aug 12 '19 at 07:59

2 Answers2

0

Because the pushid is declared inside a function and is restricted to that scope. If you want to use that value you will have to put the ajax request function inside the gonative_onesignal_info. Look for local scope vs global scope and the use of var and let keywords

Usman Abdur Rehman
  • 334
  • 1
  • 3
  • 13
-1
<?php echo $userkey; ?> or <?=$userkey; ?>

try again so you can get ..

ChrisF
  • 134,786
  • 31
  • 255
  • 325
qian
  • 1