Grab form values from html document??

Posted on 2009-04-21
Last Modified: 2013-11-05
Hi could you guys show me how to grab the values from an html form?

The form is laid out like this.
<FORM name=portRange action="PortRange.tri" method=post>
<INPUT type=hidden ame="action" value="Apply">

<INPUT type=hidden name=RuleID_0 value="0">
<INPUT maxLength=12 size=7 value="" name=name0>
<INPUT  maxLength=5 size=5 value="0" name=from0>
<INPUT maxLength=5 size=5 value="0" name=to0>
<input type=text name=pro0 value=both>
<INPUT maxLength=3 size=3 value="0" name=ip0>

<INPUT type=hidden name=RuleID_1 value="0">
<INPUT maxLength=12 size=7 value="" name=name1>
<INPUT  maxLength=5 size=5 value="0" name=from1>
<INPUT maxLength=5 size=5 value="0" name=to1>
<input type=text name=pro1 value=both>
<INPUT maxLength=3 size=3 value="0" name=ip1>

<INPUT type=hidden name=RuleID_2 value="0">
<INPUT maxLength=12 size=7 value="" name=name2>
<INPUT  maxLength=5 size=5 value="0" name=from2>
<INPUT maxLength=5 size=5 value="0" name=to2>
<input type=text name=pro2 value=both>
<INPUT maxLength=3 size=3 value="0" name=ip2>

<INPUT type=hidden name=RuleID_3 value="0">
<INPUT maxLength=12 size=7 value="" name=name3>
<INPUT  maxLength=5 size=5 value="0" name=from3>
<INPUT maxLength=5 size=5 value="0" name=to3>
<input type=text name=pro3 value=both>
<INPUT maxLength=3 size=3 value="0" name=ip3>

<INPUT type=hidden name=RuleID_4 value="0">
<INPUT maxLength=12 size=7 value="" name=name4>
<INPUT  maxLength=5 size=5 value="0" name=from4>
<INPUT maxLength=5 size=5 value="0" name=to4>
<input type=text name=pro4 value=both>
<INPUT maxLength=3 size=3 value="0" name=ip4>

<INPUT type=hidden name=RuleID_5 value="0">
<INPUT maxLength=12 size=7 value="" name=name5>
<INPUT  maxLength=5 size=5 value="0" name=from5>
<INPUT maxLength=5 size=5 value="0" name=to5>
<input type=text name=pro5 value=both>
<INPUT maxLength=3 size=3 value="0" name=ip5>
<input type="submit">

I need to know how I can parse this document and grab the value for each form element. This document will change in size and may have other lines not shown here.
Question by:hilltop
    LVL 39

    Accepted Solution

    The best way to do this I think is to use a proper HTML parser. Writing one yourself is not going to be simple but fortunately there are plenty of free ones to choose from. I can recommend the El Kabong HTML parser. It's free , very small, very quick and comes with the source code in case you want to learn and tinker. It's a Sax style parser, which means it parses the HTML and calls back to a callback function when it's found something of interest. You just write the callback function to handle this. It'll be pretty simple to hook this library in and implement a solution (I know, I've used it quite a few times myself).
    LVL 39

    Assisted Solution

    >>> The best way to do this I think is to use a proper HTML parser.
    though evilrix is right with that you could try the following quick-and-dirty approach:
    #include <string>
    #include <map>
    using namespace std;
       int pos1 = 0;
       int pos2 = 0;
       int pos3 = 0;
       int pos4 = 0;
       int pos5 = 0;
       int len  = 0;
       map<string, string> valuemap;
       string entry;
       string name;
       string value;
       string html; // = getHtml();   // get all html code somehow
       while ((pos2 = (int)html.find("<INPUT ", pos1)) != string::npos)
          pos1 = pos2 + 1;
          pos3 = (int)html.find(">", pos2);
          if (pos3 == string::npos) continue;
          entry = html.substr(pos1, pos3 - pos1); 
          // e.g. INPUT  maxLength=5 size=5 value="0" name=from4
          pos4 = (int)entry.find("name=");
          if (pos4 == string::npos) continue;
          pos4 += 5;
          pos5 = (int)entry.find(" ", pos4);
          if (pos5 == string::npos) pos5 = (int)entry.size();
          name = entry.substr(pos4, pos5-pos4);
          pos4 = (int)entry.find("value=");
          if (pos4 == string::npos) continue;
          pos4 += 5;
          pos5 = (int)entry.find(" ", pos4);
          if (pos5 == string::npos) pos5 = (int)entry.size();
          value = entry.substr(pos4, pos5-pos4);
          len = (int)value.size();
          if (len > 2 && value[0] == '"' && value[--len] == '"')
             value = value.substr(1, --len);
          valuemap[name] = value;

    Open in new window

    LVL 49

    Expert Comment

    I think you might be wanting to get the values entered by the user.  Is that correct?
    In that case, your best bet would be to get the document into a webbrowser control and use the DOM functions (like IHTMLLIElement::get_value ) to obtain the user-entered values.  That is, use the built-in functions of the IHTMLDocument2 object model.  If you waant to go this way, let me know and I'll provide some example code.
    LVL 2

    Author Comment

    No DanRollins, just what in the page. Some fileds will have data some will be blank.

    Thanks for your help I am having a look, sorry for the delay I have been very sick.
    LVL 49

    Expert Comment

    Just to help us understand the problem... What do you want to do with this information?  In the example, the value fields are all set to 0 or "" -- which might not be so useful to know.
    LVL 2

    Author Comment

    Basically I will pull this page from a router, build a http post with the data and submit it back to the device. It will provide a means to programatically add or remove an entry etc. I need whats in the page etc, not whats typed in a form.
    LVL 39

    Expert Comment

    Did you look at El Kabong as I suggested in my first post? Like I said it's a trivial SAX style HTML parser that should make extracting the data you want trivial. Have you just overlooked it as a solution or dismissed it? IF the latter I'd be interested to know why because implementing El Kabong will be trivial compared to writing your own HTML parser to extract data (the HTML standard is so hideous that the only thing about it that's standard is how unstandardized it is).
    LVL 2

    Author Comment

    I'm a little under the weather, but I do intend to look.
    LVL 39

    Expert Comment

    >> I'm a little under the weather, but I do intend to look.
    No worries... I do hope you feel better real soon.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now