form_submit.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include "mongoose.h"
00004 
00005 static const char *html_form =
00006   "<html><body>POST example."
00007   "<form method=\"POST\" action=\"/handle_post_request\">"
00008   "Input 1: <input type=\"text\" name=\"input_1\" /> <br/>"
00009   "Input 2: <input type=\"text\" name=\"input_2\" /> <br/>"
00010   "<input type=\"submit\" />"
00011   "</form></body></html>";
00012 
00013 static void send_reply(struct mg_connection *conn) {
00014   char var1[500], var2[500];
00015 
00016   if (strcmp(conn->uri, "/handle_post_request") == 0) {
00017     // User has submitted a form, show submitted data and a variable value
00018     // Parse form data. var1 and var2 are guaranteed to be NUL-terminated
00019     mg_get_var(conn, "input_1", var1, sizeof(var1));
00020     mg_get_var(conn, "input_2", var2, sizeof(var2));
00021 
00022     // Send reply to the client, showing submitted form values.
00023     // POST data is in conn->content, data length is in conn->content_len
00024     mg_send_header(conn, "Content-Type", "text/plain");
00025     mg_printf_data(conn,
00026                    "Submitted data: [%.*s]\n"
00027                    "Submitted data length: %d bytes\n"
00028                    "input_1: [%s]\n"
00029                    "input_2: [%s]\n",
00030                    conn->content_len, conn->content,
00031                    conn->content_len, var1, var2);
00032   } else {
00033     // Show HTML form.
00034     mg_send_data(conn, html_form, strlen(html_form));
00035   }
00036 }
00037 
00038 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
00039   if (ev == MG_REQUEST) {
00040     send_reply(conn);
00041     return MG_TRUE;
00042   } else if (ev == MG_AUTH) {
00043     return MG_TRUE;
00044   } else {
00045     return MG_FALSE;
00046   }
00047 }
00048 
00049 int main(void) {
00050   struct mg_server *server = mg_create_server(NULL, ev_handler);
00051 
00052   mg_set_option(server, "listening_port", "8080");
00053 
00054   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
00055   for (;;) {
00056     mg_poll_server(server, 1000);
00057   }
00058 
00059   mg_destroy_server(&server);
00060 
00061   return 0;
00062 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02