Click here to Skip to main content
Licence Public Domain
First Posted 15 Jul 2000
Views 152,198
Bookmarked 69 times

Processing HTML Forms From a CHtmlView

By Ted Crow | 15 Jul 2000
A simple method to processing HTML forms From a CHtmlView
1 vote, 8.3%
1

2
1 vote, 8.3%
3
2 votes, 16.7%
4
8 votes, 66.7%
5
4.59/5 - 29 votes
1 removed
μ 4.44, σa 2.15 [?]
  • 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 New23:04 18 Oct '10  
    Questionhow to send form automatically? Pinmemberkzfid2:32 9 Feb '06  
    QuestionIHtmlDocument2::get_cookie does not returns other cookie info? Pinmembershah_amish_b2:28 22 Apr '04  
    GeneralApplication ERROR PinmemberBalkrishna Talele3:03 14 Apr '04  
    QuestionHow to set the form values? PinmemberSunSoft20005:26 23 Oct '03  
    QuestionHow to set the form values? PinmemberSunSoft20005:26 23 Oct '03  
    GeneralSpecial chars & forreign languages PinmemberGernot Frisch0:11 29 Sep '03  
    GeneralBind to SocketAddress PinmemberManfred Hackstock7:34 4 Sep '01  
    GeneralFirst-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. PinmemberVicente Manuel Arévalo Espejo13:07 18 Jun '01  
    GeneralRe: First-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. PinmemberJC Lewis10:55 20 Jun '02  
    GeneralDHTMLUI Library Released to CodeProject PinmemberTed Crow5:46 23 Jan '01  
    GeneralRe: DHTMLUI Library Released to CodeProject PinmemberAlex Evans13:04 17 Aug '04  
    GeneralNew Class Library Update PinmemberTed Crow7:18 19 Oct '00  
    GeneralRe: New Class Library Update PinmemberJohnas Cukier8:37 5 Jan '01  
    GeneralRe: New Class Library Update PinmemberTed Crow15:32 18 Jan '01  
    QuestionWhy don't you go directly to the point? PinsussC. 'Sardaukar' Amarie21:56 27 Jul '00  
    AnswerRe: Why don't you go directly to the point? PinsussTed Crow9:21 11 Aug '00  
    GeneralJavascript/cookie PinsussDavid Coper10:53 18 Jul '00  
    GeneralRe: Javascript/cookie PinsussTed Crow15:01 18 Jul '00  
    GeneralRe: Javascript/cookie PinsussJose23:48 4 Sep '00  
    QuestionVB/JavaScript Example? PinsussPaul4:07 18 Jul '00  
    AnswerRe: VB/JavaScript Example? PinsussTed Crow14:54 18 Jul '00  
    GeneralRe: Even better..? PinsussRay22:58 19 Jul '00  
    GeneralRe: Even better..? PinsussTed Crow11:04 20 Jul '00  
    GeneralRe: Even better..? Pinsusssteve15: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.120206.1 | Last Updated 16 Jul 2000
    Article Copyright 2000 by Ted Crow
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid