Skip to content
Advertisement

Sending variables to a php script for database from java script

First, I have looked at just about every other question here on this subject but I can’t find anything like I need. I’m trying to send the userId for one signal to a PHP script to put it in a database. I have tried many different versions of this.

Here is the script that does send to the PHP script and put’s it in the database but the userId is NULL.

//Secondly this will check when subscription changed
    OneSignal.push(function() {
        OneSignal.on('subscriptionChange', function (isSubscribed) {
            if(isSubscribed==true){
                OneSignal.getUserId().then(function(userId) {
                    useragentid = userId;
                    var theuserid = userId
                }).then(function(){
                 // this is custom function
                // here you can send post request to php file as well.
                    OneSignalUserSubscription(useragentid);
                    //document.cookie = "theplayerID="+useragentid+";";

                    var theuserid = OneSignalUserSubscription(useragentid);
                });

window.location.href = "sub_post.php?userId=" + theuserid + "&sponsor=josh"; 

                document.getElementById('successtext').style.display = 'block';
                document.getElementById('unsubscribe').style.display = '';
                document.getElementById('subscribe').style.display = 'none';
            }
            else if(isSubscribed==false){
                OneSignal.getUserId().then(function(userId) {
                    useragentid = userId;
                });
                document.getElementById('unsubscribe').style.display = 'none';
                document.getElementById('subscribe').style.display = '';
            }
            else{
                console.log('Unable to process the request');
            }
        });
    });

The part to look at is this:

window.location.href = "sub_post.php?userId=" + useragentid + "&sponsor=josh"; 

You will notice that I have a var theuserid = userId in there. When I replace useragentid with theuserid in that one line, it don’t send anything over, it’s like it ignores the entire statement. But when I use useragentid it does send to the PHP script but it’s NULL. Why would it work with useragentid but not with theuserid? Is it because one is global and the other is not? Would that cause it to completely ignore the statement when using theuserid because it’s not global?

I’m hoping someone can help with this without the entire code because I’m thinking it’s a simple java script error that I’m causing so I’m thinking that the entire code is really not relevent.

Advertisement

Answer

SOLUTION:

OneSignal.getUserId(function(userId) {
window.location.href = "sub_post.php?userId=" + userId + "&sponsor=josh"; 
});

This allows me to get the userId again in a function and then send it to the PHP script which captures it successfully.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement