Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / MFC
Article

Processing HTML Forms From a CHtmlView

Rate me:
Please Sign up or sign in to vote.
4.59/5 (12 votes)
15 Jul 2000Public Domain 195.2K   3.3K   70   30
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


    Written By
    Help desk / Support Verint Systems, Inc.
    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

    Comments and Discussions

     
    GeneralForm submit to server Pin
    Ajay Kale New18-Oct-10 22:04
    Ajay Kale New18-Oct-10 22:04 
    Questionhow to send form automatically? Pin
    kzfid9-Feb-06 1:32
    kzfid9-Feb-06 1:32 
    QuestionIHtmlDocument2::get_cookie does not returns other cookie info? Pin
    Amish shah22-Apr-04 1:28
    Amish shah22-Apr-04 1:28 
    GeneralApplication ERROR Pin
    Balkrishna Talele14-Apr-04 2:03
    Balkrishna Talele14-Apr-04 2:03 
    QuestionHow to set the form values? Pin
    SunSoft200023-Oct-03 4:26
    SunSoft200023-Oct-03 4:26 
    QuestionHow to set the form values? Pin
    SunSoft200023-Oct-03 4:26
    SunSoft200023-Oct-03 4:26 
    GeneralSpecial chars &amp; forreign languages Pin
    Gernot Frisch28-Sep-03 23:11
    Gernot Frisch28-Sep-03 23:11 
    GeneralBind to SocketAddress Pin
    4-Sep-01 6:34
    suss4-Sep-01 6:34 
    GeneralFirst-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. Pin
    Vicente Arevalo18-Jun-01 12:07
    Vicente Arevalo18-Jun-01 12:07 
    GeneralRe: First-chance exception in TestHTML.exe (GDI32.DLL): 0xC0000005: Access Violation. Pin
    JC Lewis20-Jun-02 9:55
    JC Lewis20-Jun-02 9:55 
    GeneralDHTMLUI Library Released to CodeProject Pin
    Ted Crow23-Jan-01 4:46
    professionalTed Crow23-Jan-01 4:46 
    GeneralRe: DHTMLUI Library Released to CodeProject Pin
    Alex Evans17-Aug-04 12:04
    Alex Evans17-Aug-04 12:04 
    GeneralNew Class Library Update Pin
    Ted Crow19-Oct-00 6:18
    professionalTed Crow19-Oct-00 6:18 
    GeneralRe: New Class Library Update Pin
    Johnas Cukier5-Jan-01 7:37
    Johnas Cukier5-Jan-01 7:37 
    GeneralRe: New Class Library Update Pin
    Ted Crow18-Jan-01 14:32
    professionalTed Crow18-Jan-01 14:32 
    QuestionWhy don't you go directly to the point? Pin
    Sardaukar27-Jul-00 20:56
    Sardaukar27-Jul-00 20:56 
    You can use a sink to get all the events you need and rewrite the (let's call it) ISinkDispatcher's Invoke method.
    As a brutal way, I "invented" and INVOKEPARAM structure (which contains all the parameters of Invoke, ISinkDispatcher
    being derived from DWebBrowserEvents2 interface), use another interface (let's call it) IWebOleClientSite
    (derived from IOleClientSite, IOleInPlaceSite and IDocHostUIHandler) as manager of your client site and you can
    intercept ISinkDispatcher::Invoke by filling an INVOKEPARAM structure and sending in as LPARAM in an user message to
    the parent window. That could be something like:

    HRESULT STDMETHODCALLTYPE
    ISinkDispatcher::Invoke(DISPID dispId, REFIID riid, LCID lcid, WORD wFlags,
    DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
    {
    HRESULT hr = E_NOTIMPL;

    IWebOleClientSite *pCS = (IWebOleClientSite *)GetClientSite();
    if(pCS)
    {
    INVOKEPARAM InvokeParam;

    InvokeParam.dispId = dispId;
    InvokeParam.lcid = lcid;
    InvokeParam.wFlags = wFlags;
    InvokeParam.pDispParams = pDispParams;
    InvokeParam.pVarResult = pVarResult;
    InvokeParam.pExcepInfo = pExcepInfo;
    InvokeParam.puArgErr = puArgErr;

    SendMessage
    (
    pCS->m_hWndOwner,
    WM_USER_DISPINVOKE,
    (WPARAM)dispId,
    (LPARAM)&InvokeParam
    );
    }

    return hr;
    }

    (or invent a new notifications and send/post it using WM_NOTIFY, since the Web Browser is usually hosted by another
    window as parent - usually a dialog - and in parent window catch WM_NOTIFY with your user notification code, as in
    ordinary Windows WM_NOTIFY approach).

    Read DWebBrowserEvents2 to see what you have to return, the header file <dispid.h> is also very useful, or use notify
    reflection, or anything else. But that could be a way (the rest are details).

    Sooner I'll post a sample "from the scratch", using C++ but no class libraries, about how to host a WebBrowser control inside a dialog hosted by a MDI child window. Stay tooned
    AnswerRe: Why don't you go directly to the point? Pin
    Ted Crow11-Aug-00 8:21
    professionalTed Crow11-Aug-00 8:21 
    GeneralJavascript/cookie Pin
    David Coper18-Jul-00 9:53
    sussDavid Coper18-Jul-00 9:53 
    GeneralRe: Javascript/cookie Pin
    Ted Crow18-Jul-00 14:01
    professionalTed Crow18-Jul-00 14:01 
    GeneralRe: Javascript/cookie Pin
    jose4-Sep-00 22:48
    jose4-Sep-00 22:48 
    QuestionVB/JavaScript Example? Pin
    Paul Wolfensberger18-Jul-00 3:07
    Paul Wolfensberger18-Jul-00 3:07 
    AnswerRe: VB/JavaScript Example? Pin
    Ted Crow18-Jul-00 13:54
    professionalTed Crow18-Jul-00 13:54 
    GeneralRe: Even better..? Pin
    Ryan Park19-Jul-00 21:58
    Ryan Park19-Jul-00 21:58 
    GeneralRe: Even better..? Pin
    Ted Crow20-Jul-00 10:04
    professionalTed Crow20-Jul-00 10:04 
    GeneralRe: Even better..? Pin
    Steve22-Jul-00 14:13
    Steve22-Jul-00 14:13 

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

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