Click here to Skip to main content
11,413,961 members (71,380 online)
Click here to Skip to main content

Processing HTML Forms From a CHtmlView

, 15 Jul 2000 Public Domain
Rate this:
Please Sign up or sign in to vote.
A simple method to processing HTML forms From a CHtmlView
<!-- Download Links -->
  • Download demo project - 59 Kb
  • <!-- Main HTML starts here -->

    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

    Share

    About the Author

    Ted Crow
    Systems Engineer
    United States United States
    Ted has been programming computers since 1981 and networking them since 1984. He has been a professional technology manager and computer networking consultant for more than 20 years.

    Feel free to connect with Ted on LinkedIn
    Follow on   Twitter   LinkedIn

    Comments and Discussions

     
    GeneralForm submit to server Pin
    Ajay Kale New, 18-Oct-10 23:04
    memberAjay Kale New18-Oct-10 23:04 
    Questionhow to send form automatically? Pin
    kzfid, 9-Feb-06 2:32
    memberkzfid9-Feb-06 2:32 
    QuestionIHtmlDocument2::get_cookie does not returns other cookie info? Pin
    shah_amish_b, 22-Apr-04 2:28
    membershah_amish_b22-Apr-04 2:28 
    GeneralApplication ERROR Pin
    Balkrishna Talele, 14-Apr-04 3:03
    memberBalkrishna Talele14-Apr-04 3:03 
    QuestionHow to set the form values? Pin
    SunSoft2000, 23-Oct-03 5:26
    memberSunSoft200023-Oct-03 5:26 
    QuestionHow to set the form values? Pin
    SunSoft2000, 23-Oct-03 5:26
    memberSunSoft200023-Oct-03 5:26 
    GeneralSpecial chars &amp; forreign languages Pin
    Gernot Frisch, 29-Sep-03 0:11
    memberGernot Frisch29-Sep-03 0:11 
    GeneralBind to SocketAddress Pin
    Manfred Hackstock, 4-Sep-01 7:34
    memberManfred Hackstock4-Sep-01 7:34 
    GeneralFirst-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. Pin
    Vicente Manuel Arévalo Espejo, 18-Jun-01 13:07
    memberVicente Manuel Arévalo Espejo18-Jun-01 13:07 
    GeneralRe: First-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. Pin
    JC Lewis, 20-Jun-02 10:55
    memberJC Lewis20-Jun-02 10:55 
    GeneralDHTMLUI Library Released to CodeProject Pin
    Ted Crow, 23-Jan-01 5:46
    memberTed Crow23-Jan-01 5:46 
    GeneralRe: DHTMLUI Library Released to CodeProject Pin
    Alex Evans, 17-Aug-04 13:04
    memberAlex Evans17-Aug-04 13:04 
    GeneralNew Class Library Update Pin
    Ted Crow, 19-Oct-00 7:18
    memberTed Crow19-Oct-00 7:18 
    GeneralRe: New Class Library Update Pin
    Johnas Cukier, 5-Jan-01 8:37
    memberJohnas Cukier5-Jan-01 8:37 
    GeneralRe: New Class Library Update Pin
    Ted Crow, 18-Jan-01 15:32
    memberTed Crow18-Jan-01 15:32 
    QuestionWhy don't you go directly to the point? Pin
    C. 'Sardaukar' Amarie, 27-Jul-00 21:56
    sussC. 'Sardaukar' Amarie27-Jul-00 21:56 
    AnswerRe: Why don't you go directly to the point? Pin
    Ted Crow, 11-Aug-00 9:21
    sussTed Crow11-Aug-00 9:21 
    GeneralJavascript/cookie Pin
    David Coper, 18-Jul-00 10:53
    sussDavid Coper18-Jul-00 10:53 
    GeneralRe: Javascript/cookie Pin
    Ted Crow, 18-Jul-00 15:01
    sussTed Crow18-Jul-00 15:01 
    GeneralRe: Javascript/cookie Pin
    Jose, 4-Sep-00 23:48
    sussJose4-Sep-00 23:48 
    QuestionVB/JavaScript Example? Pin
    Paul, 18-Jul-00 4:07
    sussPaul18-Jul-00 4:07 
    AnswerRe: VB/JavaScript Example? Pin
    Ted Crow, 18-Jul-00 14:54
    sussTed Crow18-Jul-00 14:54 
    GeneralRe: Even better..? Pin
    Ray, 19-Jul-00 22:58
    sussRay19-Jul-00 22:58 
    GeneralRe: Even better..? Pin
    Ted Crow, 20-Jul-00 11:04
    sussTed Crow20-Jul-00 11:04 
    GeneralRe: Even better..? Pin
    steve, 22-Jul-00 15:13
    susssteve22-Jul-00 15:13 
    GeneralRe: VB/JavaScript Example? Pin
    Paul, 24-Jul-00 4:28
    sussPaul24-Jul-00 4:28 
    GeneralRe: VB/JavaScript Example? Pin
    Ted Crow, 25-Jul-00 5:04
    sussTed Crow25-Jul-00 5:04 
    GeneralRe: VB/JavaScript Example? Pin
    Paul, 30-Jul-00 6:39
    sussPaul30-Jul-00 6:39 
    GeneralA big thank-you Pin
    Simon Brown, 17-Jul-00 4:17
    sussSimon Brown17-Jul-00 4:17 
    GeneralRe: A big thank-you Pin
    Ted Crow, 18-Jul-00 15:06
    sussTed Crow18-Jul-00 15:06 

    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.

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