Questions › I'm trying to get IP address value printed into 4 box's on some Html web page

I have string with "." in the address trying to use and Java script with String Tokenizer and slip each address into each box

Source C file which passes the values when requested by the html page (ipcfg/ipcfg_get)

/*
* { "ipv4": {
*  "addr": "192.168.1.1",
*  "mask": "255.255.255.0",
*  "gw": "192.168.1.1" }
* }
*/
cp = s;
cp += sprintf(cp, "{ \"ipv4\": {");
cp += sprintf(cp, "\"addr\": \"%d.%d.%d.%d\",",
        IP4_ADDR1(ipv4_addr), IP4_ADDR2(ipv4_addr),
        IP4_ADDR3(ipv4_addr), IP4_ADDR4(ipv4_addr));
cp += sprintf(cp, "\"mask\": \"%d.%d.%d.%d\",",
        IP4_ADDR1(ipv4_mask), IP4_ADDR2(ipv4_mask),
        IP4_ADDR3(ipv4_mask), IP4_ADDR4(ipv4_mask));
cp += sprintf(cp, "\"gw\": \"%d.%d.%d.%d\"",
        IP4_ADDR1(gw_addr), IP4_ADDR2(gw_addr),
        IP4_ADDR3(gw_addr), IP4_ADDR4(gw_addr));
cp += sprintf(cp, "}}");   

HTML Program to get the values from the C file calling the function and no its not brackets or closing arguments.

<script>      
function ipcfg_get () 
{
        $.ajax({
            type: 'GET',
            url: '/ipcfg/ipcfg_get.cgi',
            dataType: 'json',
            success: function(data) {
                var s = 'addr: ' + data.ipv4.addr;
                s += ', mask: '+ data.ipv4.mask;
                s += ', gw: ' + data.ipv4.gw;
            },
       });
}

$(function() {
    $('#btnreset').on('click', ipcfg_get);
$('#btnapply').on('click', ipcfg_set);
});
</script>
Comments :
P. Jairaj replied

where do you want to print?

DrCord replied

you should at least try and define ipcfg_set to show that you have tried something yourself...much more likely to get an answer instead of downvotes.

jackotonye replied

The top file is the ipcfg.c which contains this definition int ipcfg_get_cgi(struct httpctl * http)


1 Answers :
Koustav Ray answered

In your success function do this instead of doing string operations on s. (J S O N after all stands for JavaScriptObjectNotation so treat the variables as js objects and you shall be fine.)

var s={};
s.addr=data.ipv4.addr;
s.mask=data.ipv4.mask;
s.gw=data.ipv4.gw;
//do something with s after this like put it into some div
alert(s);

jackotonye replied
I wan to be able to output each address (addr, mask, gw) on separate html text inputs. Like with ip address;
jackotonye replied
<table class="formgrid"> <tr><th>IP Address:</th><td><input type="text" name="a1" id="ip1" size="3" value="0">. <input type="text" name="a2" size="3" id="ip2" value="0">. <input type="text" name="a3" size="3" id="ip3" value="0">. <input type="text" name="a4" size="3" id="ip4" value="0"></td></tr>;
Koustav Ray replied
Not very clear what you require,but i guess you could do something like this for each ip: $('#ip1').val(s.addr); $('#ip2').val(s.mask); $('#ip3').val(s.gw);;
Koustav Ray replied
N.B. If you want to split each ip then you could do something like var splittedAddr=s.addr.toString().split('.'); and then can access each element of that array as splittedAddr[0],splittedAddr[1]..;