Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda