Overview
Features
Download
Documentation
Community
Add-Ons & Services

httpformserver questions

Please post support and help requests here.

httpformserver questions

Postby Sqarzz » 09 Apr 2012, 11:35

Hi there,
I have just started using this wonderfull set of library's but I had some questions.
I had hoped you could help me.

I started with the example HTTPFormServer:

This is the actual part I am interested in;
Code: Select all
"<form method=\"GET\" action=\"/form\">\n"
"<input type=\"text\" name=\"text\" size=\"31\">\n"
"<input type=\"text\" name=\"text2\" size=\"31\">\n"
"<input type=\"submit\" value=\"GET\">\n"
"</form>\n"


FIrst question:
when the submit buttin gets pushed, I think these fields are pushed into the object form. Is this correct?
I made this conclusion because of the if (!form.empty()) line where the fields are printed on the page.

if so : question 1.2:
Form is an object of the type HTMLForm. the method
Code: Select all
void load(const HTTPRequest& request);

is used to read the data from the GET request.
From where is this read? Only from the url?

thanks in advance
Stephen
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby rakesh » 09 Apr 2012, 16:34

For GET requests, all form key-value pairs are passes as part of the URL. POST requests pass the same information as part of the HTTP request body.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: httpformserver questions

Postby Sqarzz » 09 Apr 2012, 21:34

And does the httpformserver always has a single form that is displayed?
if a POST requests is passed with the HTTP request body, and matches a specific creteria,
could I then open a new form/page?
The content of a form is defined in the class FormRequestHandler.
If i wanted to load another page,after the first form,
I would have to create a second object of the class formRequestHandler, am I correct?
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby rakesh » 09 Apr 2012, 22:20

You would need to either render the contents of the new page from the GET/POST request handler instance, or send a redirect response to a new URL that will render the next page you wish.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: httpformserver questions

Postby Sqarzz » 09 Apr 2012, 23:45

would you want to push me in the wright direction for getting the redirect response to work?

Code: Select all
class RedirectRequestHandler: public AbstractHTTPRequestHandler
      {
      public:
            void run()
            {
                  response().redirect("http://www.google.com/");
            }
      };

I suppose this is the class i would need to use?

Inside the HTTPRequestHndler I added these:
Code: Select all
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
cout<<request.getURI();
if (request.getURI() == "/")
    return new FormRequestHandler;
else if (request.getURI() == "/redirect")
    return new RedirectRequestHandler();
}

i understood that by editing the form i could set to where te page is redirected.
Code: Select all
"<form method=\"POST\" action=\"/redirect\">\n"
... [/form]

The problem now is that the page is redirected wright after the submit button of the form is pushed.
How would i sent the page to /redirect, but only at the moment of my choice.
Thanks for your time by the way..
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby rakesh » 10 Apr 2012, 20:53

You post you form to the path where you want, and then after processing the post request, you can perform the redirect. For instance, in your createRequestHandler, you can process the form and then return the redirect handler instance.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: httpformserver questions

Postby Sqarzz » 11 Apr 2012, 11:30

Ok Thanks Rakesh, really helped!

If i would want to send to another local page (f.e "/local"), are there other methods then the "action=\"/local\"" in a form.
I was thinking of using setURI(), but it has to be invoced as a method of an object of the type HTTPRequestHandler*.
Can I make an instance of httprequesthandler, and just use the setURI function ( all this inside another handler? ( FormRequestHandler))
or is there an easier way?

thanks!
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby rakesh » 11 Apr 2012, 16:19

I am not sure what exactly you are trying to accomplish. Perhaps I can comment if you describe the workflow you are trying to achieve.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: httpformserver questions

Postby Sqarzz » 14 Apr 2012, 11:58

Well, based on the HTTPFormServer I have this handler:
Code: Select all
class FormRequestHandler: public HTTPRequestHandler
{
public:
   FormRequestHandler()  { }
   
   void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
   {
      Application& app = Application::instance();
      app.logger().information("Request from " + request.clientAddress().toString());      
      HTMLForm form(request, request.stream());
      response.setChunkedTransferEncoding(true);
      response.setContentType("text/html");
      std::ostream& ostr = response.send();


Then the important part of the html code:

Code: Select all
ostr <<
         ...
         "<form method=\"POST\" action=\"/\">\n"
         "Number: <input type=\"text\" name=\"number\" size=\"15\">\n"
         "<input type=\"submit\" value=\"INPUT\">\n"
         "</form>\n";
         ...


When checking the input I would like to redirect to other html handlers:

Code: Select all
NameValueCollection::ConstIterator it = request.begin();
NameValueCollection::ConstIterator end = request.end();

string temp1, temp2, num;
if (!form.empty())
{
   it = form.begin();
   end = form.end();
   for (; it != end; ++it)
   {
      temp1=it->first;
      temp2=it->second;
      if (temp1=="number") {num=temp2;}
   }
...


This part is where i need help:

if (num.empty()) {ostr<<"No input!\n";}
else if (num=="1") {GO TO \FORM1 (class Form1Handler: public HTTPRequestHandler in my code)}
else if (num=="2") {GO TO \FORM2 (class Form2Handler: public HTTPRequestHandler in my code)}
else { GO TO \FORM3 and give num as a parameter with the handler

So basically I would want like to know:
1. how you call another htmlformhandler.
2. how to give a parameter with the handler ( num in my example).
I suppose I just add a public variable in the last handler and set it in an objet before calling the handler?

I hope this is a little understandable?
Thanx in advance!
Sqarzz
 
Posts: 24
Joined: 09 Apr 2012, 11:11
Location: Belgium

Re: httpformserver questions

Postby rakesh » 14 Apr 2012, 19:43

It would be more efficient to directly call your other form handlers directly after setting any relevant data member values. You can also do a HTTP redirect, which will have the effect of also changing the browser URL to the new one (which may or may not be important).
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Next

Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests