skip to Main Content

I know it seems basic, but hear me out, I’ve been trying to figure it out for a good chunk of the day and something just isn’t working.

I’m trying to replace the value of a hidden form field with the visitors IP address. This is the field in question:

    <label class="previewLabel" for="awf_field-106493013">ipaddress:</label>
    <div class="af-textWrap"><input type="text" id="awf_field-106493013" class="text" name="custom ipaddress" value=""  onfocus=" if (this.value == '') { this.value = ''; }" onblur="if (this.value == '') { this.value='';} " tabindex="503" /></div>

And this is the code I have on my page currently:

    <script type="text/javascript">
  function getUrlParam(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[[]]/g, '\$&');
            var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/+/g, ' '));
        }



        var tid = getUrlParam("adid");
            var date = Date(Date.now());
        jQuery(document).ready(function () {
           (function (){  
             jQuery.ajax({
                   async: false,
                   type: 'GET',
                   url: 'https://api.ipify.org/?format=json',
                   success: function(data) {
                        //callback
                     console.log(data.ip) //This works properly;
                            jQuery('#awf_field-106493013').val(data.ip); //This does not work
                   }
              });
                 }());


           jQuery('#awf_field-106493014').val(date.toString()); //This works properly
          if(tid != null){
           jQuery('#awf_field-106493015').val(tid.toString()); //This works properly
          }else{
            jQuery('#awf_field-106493015').val("nulltid"); //This works properly
          }

        });


</script>

I’ve also tried to make the call and replacement with jQuery.get and the replacement with document.getElementById. All other fields are replaced except for this one IP field and, as the comment in the code says, the IP is even properly printed into the console before it’s dropped while trying to be replaced.

I’ve also tried changing async from false to true, but this also did not work.

I have no idea why this is behaving how it is and would like some assistance.

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    As it turned out this particular api was returning some sort of weird json object, after reading their documentation I changed https://api.ipify.org/?format=json to https://api.ipify.org/ and it worked perfectly.


  2. Are you saying you are unable to retrieve the ip address to the text box with id “awf_field-106493013” ?

    If yes, your code works fine for me.

    https://jsfiddle.net/7yqrk1hL/

    <!DOCTYPE html>
    <html>
    
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    </head>
    
    <body>
        <label class="previewLabel" for="awf_field-106493013">ipaddress:</label>
        <div class="af-textWrap"><input type="text" id="awf_field-106493013" class="text" name="custom ipaddress" value=""
                onfocus=" if (this.value == '') { this.value = ''; }" onblur="if (this.value == '') { this.value='';} "
                tabindex="503" /></div>
        <script>
    
            $(document).ready(function () {
                (function () {
                    jQuery.ajax({
                        async: false,
                        type: 'GET',
                        url: 'https://api.ipify.org/?format=json',
                        success: function (data) {
                            //callback
                            console.log(data.ip) //This works properly;
                            jQuery('#awf_field-106493013').val(data.ip); //This does not work
                        }
                    });
                }());
    
    
                jQuery('#awf_field-106493014').val(date.toString()); //This works properly
                if (tid != null) {
                    jQuery('#awf_field-106493015').val(tid.toString()); //This works properly
                } else {
                    jQuery('#awf_field-106493015').val("nulltid"); //This works properly
                }
    
            });
    
    
        </script>
    </body>
    
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search