Grab form values from html document??

Posted on 2009-04-21
Medium Priority
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
  • 3
  • 3
  • 2
  • +1
LVL 40

Accepted Solution

evilrix earned 1000 total points
ID: 24202038
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

itsmeandnobodyelse earned 1000 total points
ID: 24202706
>>> 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

ID: 24211611
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 24272233
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

ID: 24275050
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.

Author Comment

ID: 24275134
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 40

Expert Comment

ID: 24277109
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).

Author Comment

ID: 24277165
I'm a little under the weather, but I do intend to look.
LVL 40

Expert Comment

ID: 24277176
>> 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

850 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