Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda