#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221

    Replace php $_GET with javascript


    Hi;

    I inherited some knowledge from Kicken and I am about to turn it to shet as usual.

    I give this code to clients to place in their site, pass data to me and load dynamic content:
    Code:
    <script type="text/javascript" src="https://moosh.com/countdown_timer_evergreen.php" 
    data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d" 
    data-launch_id="43" data-email="<?php echo $_GET['email'];?>">
    </script>
    Now I want to replace the $_GET php part with javascript (data-email).

    Using this javascript, I capture the email from the url so no need to use <?php echo $_GET['email'];?> anymore.
    Code:
    <script>
        var getUrlParameter = function getUrlParameter(sParam) 
          {
            var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;
            for (i = 0; i < sURLVariables.length; i++) 
              {
                sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] === sParam) 
                  {
                    return sParameterName[1] === undefined ? true : sParameterName[1];
                  }
              }
          }
        var email = getUrlParameter('email');
      </script>
    How can I update the code I give them so I capture the email from the url using the js code and pass it to them.

    I want to give them 1 piece of code. Maybe something like?

    Not sure how to pass the value of email between scripts.

    Code:
    <script type="text/javascript" src="the code hosted on my site that captures email from url"></script>
    <script type="text/javascript" src="https://moosh.com/countdown_timer_evergreen.php" 
    data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d" 
    data-launch_id="43" data-email="SOMEHOW EMAIL CAPTURED USING JS CODE?">
    </script>
    Thanks
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    You can't do it that way.

    Have you considered that maybe, just maybe, you could get the email in the code that countdown_timer_evergreen.php is outputting?
  4. #3
  5. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by requinix
    You can't do it that way.

    Have you considered that maybe, just maybe, you could get the email in the code that countdown_timer_evergreen.php is outputting?
    Wo wait. countdown_timer_evergreen.php expects 3 parameters to load the data. data-email is one of them. It has to be passed to countdown_timer_evergreen.php.
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    countdown_timer_evergreen.php will not be able to see those attributes. At all. It can't possibly need them because it can't possibly get them.

    What it could do (is doing) is output Javascript code and that can get the attributes. But using Javascript to set grab the email and set the attribute and then get that attribute back is silly. So the code gets the email from the URL and that's it. No data-email attribute.
  8. #5
  9. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by requinix
    countdown_timer_evergreen.php will not be able to see those attributes. At all. It can't possibly need them because it can't possibly get them.

    What it could do (is doing) is output Javascript code and that can get the attributes. But using Javascript to set grab the email and set the attribute and then get that attribute back is silly. So the code gets the email from the URL and that's it. No data-email attribute.
    Remember this?

    countdown_timer_evergreen.php

    PHP Code:
    <?php 
    //Allow cross-origin requests 
    header('Access-control-allow-origin: *'); 

    $action = isset($_GET['action'])?$_GET['action']:null
    switch (
    $action){ 
        case 
    'load-template'
            
    DoLoadTemplate(); 
            break; 
        default: 
            
    DoDefault(); 

    exit;  

    function 
    DoDefault(){ 
        
    header('Content-type: text/javascript'); 

    ?>
    (function(window){ 
        var currentScript = document.currentScript; 
        var apiUrl = currentScript.src; 

        if (!('jQuery' in window)){ 
            loadJQuery(initialize); 
        } else { 
            initialize(); 
        } 

        function loadJQuery(cb){ 
            var script = document.createElement('script'); 
            script.src = 'https://code.jquery.com/jquery-3.3.1.min.js'; 
            script.type = 'text/javascript'; 
            script.addEventListener('load', cb); 
            document.getElementsByTagName('head')[0].appendChild(script); 
        } 

        function initialize(){ 
            var $currentScript = $(currentScript); 

            var params = $.param({ 
                action: 'load-template' 
                , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
                , launch_id: $currentScript.data('launch_id') 
                , email: $currentScript.data('email') 
            }); 
            console.log(params);

            $.get(apiUrl, params).then(function(html){ 
                var div = $('<div>').html(html); 
                $currentScript.after(div); 
            }); 
        } 
    }(this)); 
    <?php 


    function 
    DoLoadTemplate()
        { 
            
    header('Content-type: text/html; charset=utf-8'); 
            
    $launch_owner_email_hashed htmlspecialchars($_GET['launch_owner_email_hashed']);
            
    $launch_id htmlspecialchars($_GET['launch_id']);
            
    $email htmlspecialchars($_GET['email']);
            
        
        if(
    $_SERVER['SERVER_ADDR']=="::1")
            {
                
    $root "http://local.moosh.com/eg-launch-timer";
            }
        else 
            {
                
    $root "https://moosh.com/eg-launch-timer";
            }
        
    ?> dd
    <iframe src="<?php echo $root.'/'.$launch_owner_email_hashed.'/'.$launch_id.'/'.$email;?>" style="width:100%; height:100%; border:none;">    
    </iframe>

    <?php ?>

    You see where I am stuck?
    Last edited by English Breakfast Tea; April 30th, 2018 at 11:55 PM.
  10. #6
  11. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    How do you recommend I do it?

    The only thing I need to do is to change <?php echo $_GET['email'];?> with javascript in this piece
    Code:
    <script type="text/javascript" src="https://moosh.com/countdown_timer_evergreen.php" 
    data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d" 
    data-launch_id="43" data-email="<?php echo $_GET['email'];?>">
    </script>
  12. #7
  13. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    Do you at all understand how that Javascript works? No, you do not. What you have to do right now is learn what the code does. Every single line of it.

    To test whether you've learned, answer this question: how does the code get the email address?
  14. #8
  15. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by requinix
    Answer this question: how does the code get the email address?
    Which one? The one that looks for email in URL or Kicken's?

    The script starting with <script> var getUrlParameter, goes through the url variables and gets email by = getUrlParameter('email');

    The one that Kicken wrote for me (countdown_timer_evergreen.php), yes yes yes I don't fully understand it.

    That receives the value of the data-SOME DATA attributes from any script calling it.

    So I guess the source script that has data-something can pass anything to it.

    This seems to work but it's too much code, doesn't seem right and it will probably get you even more mad.
    Code:
    <script id="my_script" type="text/javascript" src="" data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d" data-launch_id="43" data-email= ""></script>
    <script>
          var getUrlParameter = function getUrlParameter(sParam) 
          {
            var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;
            for (i = 0; i < sURLVariables.length; i++) 
              {
                sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] === sParam) 
                  {
                    return sParameterName[1] === undefined ? true : sParameterName[1];
                  }
              }
          }
        var email = getUrlParameter('email');
        var url = email;
        var b = document.querySelector("#my_script");
        b.setAttribute("data-email", email);
    </script>
    Are you gonna kill me ?
    Last edited by English Breakfast Tea; May 1st, 2018 at 12:46 AM.
  16. #9
  17. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,271
    Rep Power
    4193
    It's not that complicated. You have Javascript code that generates your iframe tag and URL and needs an email address.

    You also have javascript code that grabs an email from the URL.

    Just combine the two. Copy/paste the URL parsing function into your current code, then replace the line that gets the email from the data attribute with the function call to get the email from the url. Easy peasy basic stuff.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  18. #10
  19. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by kicken
    It's not that complicated. You have Javascript code that generates your iframe tag and URL and needs an email address.

    You also have javascript code that grabs an email from the URL.

    Just combine the two. Copy/paste the URL parsing function into your current code, then replace the line that gets the email from the data attribute with the function call to get the email from the url. Easy peasy basic stuff.

    From what I get of Requnix's post, I might be doing something very wrong to change data-email like that.

    With my very basic knowledge of coding this code and what I see, this is doing exactly what you explained Kicken
    Code:
    <div id = "countdown_timer"><script id="my_script" type="text/javascript" src="http://local.webmoosh.com/countdown_timer_evergreen.php" data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d" data-launch_id="14" data-email= ""></script><script>
          var getUrlParameter = function getUrlParameter(sParam) 
          {
            var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;
            for (i = 0; i < sURLVariables.length; i++) 
              {
                sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] === sParam) 
                  {
                    return sParameterName[1] === undefined ? true : sParameterName[1];
                  }
              }
          }
        var email = getUrlParameter('email');
        var url = email;
        var b = document.querySelector("#my_script");
        b.setAttribute("data-email", email);
    </script></div>
    And it works, but it looks wrong to ask the user to share such code with multiple <script> tags.

    Honestly I feel bad to continue but I am very desperate for this.

    Sorry guys. I am trying.
  20. #11
  21. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    Forget about the data-email attribute. Forget it entirely. It never existed, it will never exist. You don't need it.

    Okay. I'm going to try this one more time:

    You've managed to get the email into a variable, right? Using Javascript code. Now, if I remember correctly, the end goal of this whole thing is to get the email into a variable in Javascript code, right? Can you put the 2 and 2 together now to get the right answer?
  22. #12
  23. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by requinix
    Forget about the data-email attribute. Forget it entirely. It never existed, it will never exist. You don't need it.

    Okay. I'm going to try this one more time:

    You've managed to get the email into a variable, right? Using Javascript code. Now, if I remember correctly, the end goal of this whole thing is to get the email into a variable in Javascript code, right? Can you put the 2 and 2 together now to get the right answer?
    Hey

    I got it working a 2 nights ago but I had to go to dentist for annual checkup and I didn't have time to post. I also saw my friend from last year so we had a long talk about web design.

    The code I give them is
    Code:
    <script  type="text/javascript" src="http://local.moosh.com/countdown_timer_evergreen.php" 
    data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d" 
    data-launch_id="14" ></script>
    Ok. Here are the 2 things that I was not understanding:

    1 - By using Kicken's countdown_timer_evergreen.php script, the client (or anyone else who is using that script) has access to $_GET['email'].

    You're probably thinking I TOLD YOU A 100 TIMES! I guess you did. I never thought I can have that control over another website and access $_GET that way.

    I always thought that would be a pure PHP thing and HTML or javascript can never to access it. That brings me to number 2:

    2 - The validate function in the code. I am still shocked that I can do PHP validation with that code!

    What that devil javascript is doing, is allowing them to run the query from their website. Running PHP on my site from another site.

    I am not sure, is it I or this is an unusual thing.

    2 questions:

    1 - Is this method robust?

    2 - Of course this doesn't wortk unless it's inside html tag (div, p, span etc).
    How can I make sure they embed this within some html elements?
    I thought I give them something like
    Copy and paste the code inside <div id = "countdown_timer" style="margin:0; padding:0; display:inline;"></div> tags anywhere on your website.
    Then I see it can add margin, padding, width etc.

    Here is the script.

    Thanks

    PHP Code:
    <?php
    //Allow cross-origin requests 
    header('Access-control-allow-origin: *'); 

    $action = isset($_GET['action'])?$_GET['action']:null
    switch (
    $action){ 
        case 
    'validate_date'
            
    validate();
             break; 
        case 
    'load-template'
            
    load_template(); 
            break; 
        default: 
            
    DoDefault(); 

    exit;

    function 
    DoDefault(){
        
    header('Content-type: text/javascript');
        
        
    ?>
        (function(window){
            var currentScript = document.currentScript;
            var apiUrl = currentScript.src;
            
            if (!('jQuery' in window)){
                loadJQuery(initialize);
            } else {
                initialize();
            }
            
            function loadJQuery(cb){
                var script = document.createElement('script');
                script.src = 'https://code.jquery.com/jquery-3.3.1.min.js';
                script.type = 'text/javascript';
                script.addEventListener('load', cb);
                document.getElementsByTagName('head')[0].appendChild(script);
            }

            // I added your function here
            function getUrlParameter(sParam) {
                var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                sURLVariables = sPageURL.split('&'),
                sParameterName,
                i;
                for (i = 0; i < sURLVariables.length; i++) {
                    sParameterName = sURLVariables[i].split('=');
                    if (sParameterName[0] === sParam) {
                        return sParameterName[1] === undefined ? true : sParameterName[1];
                    }
                }
            }
            // getting the email from the $_GET query here
            var email = getUrlParameter('email');
        
            function initialize(){
                

            //Validate
               var $currentScript = $(currentScript); 
                var params = $.param({ 
                    action: 'validate_date' 
                    , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
                    , launch_id: $currentScript.data('launch_id') 
                    , email: email  // and inserting it in your params here

                }); 
                $.get(apiUrl, params).then(function(html){ 
                var div = $('<div>').html(html); 
                $currentScript.after(div); 
            });


                var $currentScript = $(currentScript);
                var params = $.param({
                    action: 'load-template'
                    , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
                    , launch_id: $currentScript.data('launch_id')
                    , email: email  // and inserting it in your params here
                });
                console.log(params);
                
                $.get(apiUrl, params).then(function(html){
                    var div = $('<div>').html(html);
                    $currentScript.after(div);
                });
            }
        }(this));
        <?php
    }

    function 
    validate()
        {
            
    $launch_id htmlspecialchars($_GET['launch_id']);
            
    $prospect_email htmlspecialchars($_GET['email']);
            
    $CI =& get_instance();
            
            
    //Redriect data
            
    $query_launch_details $CI->db->get_where('launch_launches', array('id' => $launch_id));    
            
    $status $CI->prospects_library->can_prospect_see_sales_page($launch_id,$prospect_email);      
            if(
    $status==1)
                {
                    
    $CI->data['full_redirect_url'] = $query_launch_details->result_array()[0]['offer_closed_page'];
                }
            if(
    $status==3)
                {
                    
    $_SESSION['error_message'] = 'This page will be available soon';
                    
    $CI->data['full_redirect_url'] = base_url('errors');
                }
            if(
    $status==|| $status ==3)
              {
                
    $CI->data['full_redirect_url'] = $query_launch_details->result_array()[0]['offer_closed_page'];
                
    ?><script>window.location.replace("<?php echo $CI->data['full_redirect_url']?>");</script><?php
              
    }
            
    $sql ="UPDATE launch_launches set timer_loads = timer_loads + 1 WHERE id = ?";  
            
    $CI->db->query($sql, array($launch_id));
        }

    function 
    load_template()
        { 
            
    header('Content-type: text/html; charset=utf-8'); 
            
    $launch_owner_email_hashed htmlspecialchars($_GET['launch_owner_email_hashed']);
            
    $launch_id htmlspecialchars($_GET['launch_id']);
            
    $email htmlspecialchars($_GET['email']);
            
        
        if(
    $_SERVER['SERVER_ADDR']=="::1")
            {
                
    $root "http://local.moosh.com/eg-launch-timer";
            }
        else 
            {
                
    $root "https://moosh.com/eg-launch-timer";
            }
        
    ?>
    <iframe src="<?php echo $root.'/'.$launch_owner_email_hashed.'/'.$launch_id.'/'.$email;?>" style="width:400px; height:140px; border:none;">    
    </iframe>


    <?php ?>
    Last edited by English Breakfast Tea; May 2nd, 2018 at 09:00 PM.
  24. #13
  25. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,271
    Rep Power
    4193
    I don't see why you're doing two separate requests, one for validating and one to load the template. Especially since you load the template regardless of the result of your validation.

    Combine them into a single request and save a few bytes.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  26. #14
  27. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by kicken
    I don't see why you're doing two separate requests, one for validating and one to load the template. Especially since you load the template regardless of the result of your validation.

    Combine them into a single request and save a few bytes.
    Do you mean combine the functions?

    PHP Code:
    <?php 
    //Allow cross-origin requests 
    header('Access-control-allow-origin: *'); 

    $action = isset($_GET['action'])?$_GET['action']:null
    switch (
    $action){ 
        case 
    'validate_date'
            
    validate();
             break; 
        case 
    'load-template'
            
    load_template(); 
            break; 
        default: 
            
    DoDefault(); 

    exit;  

    function 
    DoDefault(){ 
        
        
    header('Content-type: text/javascript'); 
        

    ?> 
    (function(window){ 

        var currentScript = document.currentScript; 
        var apiUrl = currentScript.src; 

        if (!('jQuery' in window)){ 
            loadJQuery(initialize); 
        } else { 
            initialize(); 
        } 

        function loadJQuery(cb){ 
            var script = document.createElement('script'); 
            script.src = 'https://code.jquery.com/jquery-3.3.1.min.js'; 
            script.type = 'text/javascript'; 
            script.addEventListener('load', cb); 
            document.getElementsByTagName('head')[0].appendChild(script); 
        } 

        function initialize(){ 
            
               //Validate
               var $currentScript = $(currentScript); 
                var params = $.param({ 
                    action: 'validate_date' 
                    , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
                    , plc_id: $currentScript.data('plc_id') 
                     
                }); 
                $.get(apiUrl, params).then(function(html){ 
                var div = $('<div>').html(html); 
                $currentScript.after(div); 
            });


            var $currentScript = $(currentScript); 
            var params = $.param({ 
                action: 'load-template' 
                , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
                , plc_id: $currentScript.data('plc_id') 
                 
            }); 
            //console.log(params);

            $.get(apiUrl, params).then(function(html){ 
                var div = $('<div>').html(html); 
                $currentScript.after(div); 
            }); 
            
           
        } 
    }(this)); 
    <?php 


    function 
    validate()
        {
            
    header('Content-type: text/html; charset=utf-8'); 
            
    $launch_owner_email_hashed htmlspecialchars($_GET['launch_owner_email_hashed']);
            
    $plc_id htmlspecialchars($_GET['plc_id']);
            
    $CI =& get_instance();
            
    $redirect $CI->page_access_library->redirect_broadcast_plc($plc_id1);
            if(
    $redirect[0]!=1)
                {
                    
    ?><script>window.location.replace("<?php echo $redirect[1];?>");</script><?php
                
    }

            
    $sql ="UPDATE launch_plcs set page_loads = page_loads + 1 WHERE id = ?";  
            
    $CI->db->query($sql, array($plc_id));
        }

    function 
    load_template()
        { 
            
    header('Content-type: text/html; charset=utf-8'); 
            
    $launch_owner_email_hashed htmlspecialchars($_GET['launch_owner_email_hashed']);
            
    $plc_id htmlspecialchars($_GET['plc_id']);
            
    $root base_url("bc-launch");
        
    ?> 
    <iframe src="<?php echo $root.'/'.$launch_owner_email_hashed.'/'.$plc_id;?>" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;">    
    </iframe>

    <?php ?>

IMN logo majestic logo threadwatch logo seochat tools logo