1

I currently have a div which displays information such as DNS and IP address.

I am trying to trigger an alert if the IP Address variable changes, so I've attempted both listening for changes to the div and the variable itself without much luck. Is there an obvious way to alert or trigger a function if the variable changes?

 $(ipv4.ipaddr).change(function() {
        alert("IP Changed");
    }); 

And the script used to display the IP addr normally:

if(result.ipv4){
    $("#btn-disconnect").show();
    $("#btn-disconnect-modem").show();
    var ipv4=result.ipv4;
    html+="<tr><td>IPAddr</td><td>"+ ipv4.ipaddr+"</td></tr>";
    if(ipv4.netmask) html+="<tr><td>Netmask</td><td>"+ ipv4.netmask+"</td></tr>";
    if(ipv4.gateway) html+="<tr><td>Gateway</td><td>"+ ipv4.gateway+"</td></tr>";
    label_dns="DNS";
    for(var i=0;i<ipv4.dns.length;i++){
        html+="<tr><td>"+label_dns+"</td><td>"+ ipv4.dns[i] +"</td></tr>";
        label_dns="";
    }
    if(proto=="wwan" || proto=="tethering"||proto=="3g"){
        if(result.tx) html+="<tr><td>TX Data</td><td>"+(result.tx/1024/1024>1? ((result.tx/1024/1024).toFixed(2)+" MB"): ((result.tx/1024).toFixed(2) +" KB") )+" </td></tr>";
        if(result.rx) html+="<tr><td>RX Data</td><td>"+(result.rx/1024/1024>1? ((result.rx/1024/1024).toFixed(2)+" MB"): ((result.rx/1024).toFixed(2) +" KB") )+"</td></tr>";
    }
    if(typeof sta_connected === "function") sta_connected();

}else{
    if( (proto=="wwan" ||proto=="tethering")){
        if(!result.disabled) html+="<tr><td>Connecting to the Internet</td></tr>";
    }
    if(proto=="wisp" ||proto=="wds"){
        if(!result.disabled) html+="<tr><td>Connecting to the Internet</td></tr>";
        result.disabled? $("#btn-disconnect").hide():$("#btn-disconnect").show();
    }
}
$(section).html(html);
}
gjames
  • 149
  • 1
  • 5
  • 1
    Please include the applicable HTML section(s) to help answer your question. – JohnH Dec 28 '17 at 15:20
  • The html is literally just a div with a table, it gets populated from the script (for example html+="IPAddr"+ ipv4.ipaddr+"";) ---- This populates the table section in the body
    – gjames Dec 28 '17 at 15:37

2 Answers2

0

I need the html to give a better answer but this one I am giving you already works! I suppose you use jQuery and you must know when you want to check the ip, i mean you can do it always putting an onclick method on the body (not recommened) or you can do it when the user press any available submit button in your form (I will explain this one). Check this out:

<script>

if($("input=[type=submit]").on('click',function(){

  var lastip=document.getElementById("ip").value;
  $.post("yourfile.php", { lastip: lastip }, function(data){
    $("#yourdiv").html(data);
  });

 });

</script>

yourfile.php

   function user_ip() {
        $ip = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ip = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ip = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ip = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
           $ip = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ip = getenv('REMOTE_ADDR');
        else
            $ip = 'Not Available';
        return $ip;
    }

$lastip= $_POST['lastip'];
$nowip= user_ip();

if($nowip!=$lastip AND !empty($lastip)){

echo 'ip changed!';
exit();

}

 <div id="yourdiv">
    <table>
      <tr>
        <td id="ip">IP:<?php echo $nowip;?></td>
        <td id="dns">DNS:</td>
      </tr>
    </table>
 </div>
Art_Code
  • 518
  • 4
  • 12
0

How about something like:

$(function() {
  var ipAddress = ipv4.ipaddr
  setInterval(() => {
    if (ipAddress !== ipv4.ipaddr) {
      var ipAddress = ipv4.ipaddr
      alert('IP Has Changed!')
    }
  }, 1000)
});

This stores the current IP address in ipAddress on page load then every second it will check to make sure it's the same as the current ipv4.ipaddr and if it isn't - raise the alert saving the new value ready for the check to take place again.

Note: This assumes you're populating ipv4.ipaddr automatically somewhere.

webnoob
  • 15,747
  • 13
  • 83
  • 165