Click here to Skip to main content
Licence Public Domain
First Posted 15 Jul 2000
Views 154,482
Bookmarked 68 times

Processing HTML Forms From a CHtmlView

By | 15 Jul 2000 | Article
A simple method to processing HTML forms From a CHtmlView
  • Download demo project - 59 Kb
  • Introduction

    In my wanderings I have seen a lot of code to get HTML dialogs, html pages and the like into a C++ project but I haven't heard any mention about processing forms.  That is about to end now, the included project demonstrates (very basic) processing of information submitted by a HTML form (which loaded from the application's resources).

    The project also demonstrates the use of linked HTML pages within the application's resources.  While I did not specifically demonstrate it's use, JavaScript and/or VBScript can be used as well.  The possibility of using Java class files has also come to mind, but that is beyond the scope of this article.

    The project overrides CHtmlView::OnBeforeNavigate2 to catch the form data.  The only way to get this navigation message from the CHtmlView is to simply place an action property in the <FORM> tag like this:

     <FORM action="" method="POST" name="Test">

    The overridden code is as follows:

    void CTestHTMLView::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, 
                                          LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, 
                                          LPCTSTR lpszHeaders, BOOL* pbCancel) 
    {
    	// Are we on the initial navigation thrown by the 
    	// OnInitialUpdate?  If not, process the POST.
    	if(	m_bProcessNavigate )
    	{
    		// Build a message box from the submitted data
    		CString strMessage, strBytes;
    
    		strMessage = _T("");
    
    		// Get general info from the submitted form
    		strMessage.Format(
    		_T("Browse To:\n%s\n\nFlags: %d\nTarget Frame:\n%s\n\nHeaders:\n%s\n"),
    		lpszURL, nFlags,	lpszTargetFrameName, lpszHeaders);
    
    		// Get the POST data
    		// This is where this sample gets semi-cool
    		strBytes = _T("\n\nPosted Bytes :\n\n");
    		if (baPostedData.GetSize())
    		{
    			// This is a kludgy copy, but you get the idea
    			for(int i = 0;i < baPostedData.GetSize();i++)
    			{
    				strBytes += (char)baPostedData[i];
    			}
    			// Let's split the posted data into separate lines
    			// for the messagebox
    			strBytes.Replace("&","\n");
    		}
    
    		// Once the data is copied, we can do anything we 
    		// want with it, but I'll just display the silly
    		// MessageBox
    
    		AfxMessageBox((strMessage + strBytes),MB_OK);
    
    		// Let's NOT Navigate!
    		//*pbCancel = TRUE;
    	}
    
    	CHtmlView::OnBeforeNavigate2(lpszURL, nFlags,	
    	                             lpszTargetFrameName, baPostedData,
    	                             lpszHeaders, pbCancel);
    }

    Try the program, have a good look at the source for both the C++ and HTML, I have tried to make sure everything I did is fully documented.

    Enjoy!

    License

    This article, along with any associated source code and files, is licensed under A Public Domain dedication

    About the Author

    Ted Crow

    Chief Technology Officer

    United States United States

    Member

    Ted has been programming computers since 1981 and networking them since 1984. He has been a self-employed computer networking consultant for over 15 years.
     
    Since 2001, he has served full-time as the Information Technology Manager for a regional commercial contractor.
     
    Ted is currently redesigning the network, including VoIP, an iSCSI SAN and virtual servers in a blade environment. Most of his coding right now is targeted at automating portions of this network environment.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralForm submit to server PinmemberAjay Kale New22:04 18 Oct '10  
    Questionhow to send form automatically? Pinmemberkzfid1:32 9 Feb '06  
    Instead of using
    void Navigate2(
    LPCTSTR lpszURL,
    DWORD dwFlags = 0,
    LPCTSTR lpszTargetFrameName = NULL,
    LPCTSTR lpszHeaders = NULL,
    LPVOID lpvPostData = NULL,
    DWORD dwPostDataLen = 0
    );
    to send the post data by myself.
    Can I use anyother method to send the posted data automatically.
    What I mean is that can I still wait at CHtmlView::OnBeforeNavigate2 and just change some of the posted data but let others untounched? Hence, the main problem is that how can I press the "submit" button automatically after I receive the form.
     
    Thanks very much for any help given!

    QuestionIHtmlDocument2::get_cookie does not returns other cookie info? Pinmembershah_amish_b1:28 22 Apr '04  
    GeneralApplication ERROR PinmemberBalkrishna Talele2:03 14 Apr '04  
    QuestionHow to set the form values? PinmemberSunSoft20004:26 23 Oct '03  
    QuestionHow to set the form values? PinmemberSunSoft20004:26 23 Oct '03  
    GeneralSpecial chars & forreign languages PinmemberGernot Frisch23:11 28 Sep '03  
    GeneralBind to SocketAddress PinmemberManfred Hackstock6:34 4 Sep '01  
    GeneralFirst-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. PinmemberVicente Manuel Arévalo Espejo12:07 18 Jun '01  
    GeneralRe: First-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. PinmemberJC Lewis9:55 20 Jun '02  
    GeneralDHTMLUI Library Released to CodeProject PinmemberTed Crow4:46 23 Jan '01  
    GeneralRe: DHTMLUI Library Released to CodeProject PinmemberAlex Evans12:04 17 Aug '04  
    GeneralNew Class Library Update PinmemberTed Crow6:18 19 Oct '00  
    GeneralRe: New Class Library Update PinmemberJohnas Cukier7:37 5 Jan '01  
    GeneralRe: New Class Library Update PinmemberTed Crow14:32 18 Jan '01  
    QuestionWhy don't you go directly to the point? PinsussC. 'Sardaukar' Amarie20:56 27 Jul '00  
    AnswerRe: Why don't you go directly to the point? PinsussTed Crow8:21 11 Aug '00  
    GeneralJavascript/cookie PinsussDavid Coper9:53 18 Jul '00  
    GeneralRe: Javascript/cookie PinsussTed Crow14:01 18 Jul '00  
    GeneralRe: Javascript/cookie PinsussJose22:48 4 Sep '00  
    QuestionVB/JavaScript Example? PinsussPaul3:07 18 Jul '00  
    AnswerRe: VB/JavaScript Example? PinsussTed Crow13:54 18 Jul '00  
    GeneralRe: Even better..? PinsussRay21:58 19 Jul '00  
    GeneralRe: Even better..? PinsussTed Crow10:04 20 Jul '00  
    GeneralRe: Even better..? Pinsusssteve14:13 22 Jul '00  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.5.120529.1 | Last Updated 16 Jul 2000
    Article Copyright 2000 by Ted Crow
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid