Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
35bd2b60
Commit
35bd2b60
authored
Apr 10, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added form.c
parent
c5287b90
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
0 deletions
+84
-0
Makefile
examples/Makefile
+3
-0
form.c
examples/form.c
+81
-0
No files found.
examples/Makefile
View file @
35bd2b60
...
...
@@ -50,6 +50,9 @@ upload: upload.c ../mongoose.c
auth
:
auth.c ../mongoose.c
$(CC)
auth.c ../mongoose.c
$(OUT)
$(CFLAGS)
form
:
form.c ../mongoose.c
$(CC)
form.c ../mongoose.c
$(OUT)
$(CFLAGS)
mjpg
:
mjpg.c ../mongoose.c
$(CC)
mjpg.c ../mongoose.c
$(OUT)
$(CFLAGS)
...
...
examples/form.c
0 → 100644
View file @
35bd2b60
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"
static
int
static_value
=
123
;
// Exposed and changeable via the form
static
void
print_html_form
(
struct
mg_connection
*
conn
)
{
mg_send_header
(
conn
,
"Content-Type"
,
"text/html"
);
mg_send_header
(
conn
,
"Cache-Control"
,
"max-age=0, post-check=0, "
"pre-check=0, no-store, no-cache, must-revalidate"
);
// Note that all the following normally should reside in static HTML page
mg_printf_data
(
conn
,
"%s"
,
"<html><head>"
);
// It is better to use local copy though
mg_printf_data
(
conn
,
"<script src=
\"
%s
\"
></script>"
,
"http://code.jquery.com/jquery-1.11.0.min.js"
);
mg_printf_data
(
conn
,
"%s"
,
"<script> jQuery(function() {
\n
"
);
// Here is the ajax call that fetches data from the device and
// updates the form
mg_printf_data
(
conn
,
"%s"
,
"$.ajax({ url: '/get_value', dataType: 'json', "
"success: function(d) { $('#val').val(d.value); }});
\n
"
);
// This ajax call is triggered when submit button is pressed. It sends new
// value to the device.
mg_printf_data
(
conn
,
"%s"
,
"$(document).on('click', '#button', function() {"
" $.ajax({ url: '/set_value', dataType: 'json', "
" data: { new_value: $('#val').val() } });
\n
"
" return false; });
\n
"
);
mg_printf_data
(
conn
,
"%s"
,
"});</script>"
);
mg_printf_data
(
conn
,
"%s"
,
"</head><body>"
);
mg_printf_data
(
conn
,
"%s"
,
"<h1>Ajax form submission example</h1>"
);
mg_printf_data
(
conn
,
"%s"
,
"<form>"
);
mg_printf_data
(
conn
,
"%s"
,
"Device value: <input type=text id=val />"
);
mg_printf_data
(
conn
,
"%s"
,
"<input type=submit id=button />"
);
mg_printf_data
(
conn
,
"%s"
,
"</form>"
);
mg_printf_data
(
conn
,
"%s"
,
"</body></html>"
);
}
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_REQUEST
)
{
if
(
strcmp
(
conn
->
uri
,
"/get_value"
)
==
0
)
{
mg_printf_data
(
conn
,
"{
\"
value
\"
: %d}"
,
static_value
);
}
else
if
(
strcmp
(
conn
->
uri
,
"/set_value"
)
==
0
)
{
// This Ajax endpoint sets the new value for the device variable
char
buf
[
100
]
=
""
;
mg_get_var
(
conn
,
"new_value"
,
buf
,
sizeof
(
buf
));
// Get form variable
static_value
=
atoi
(
buf
);
// Set new value
mg_printf_data
(
conn
,
"%s"
,
"{
\"
success
\"
: true}"
);
printf
(
"Setting value to [%d]
\n
"
,
static_value
);
// Debug trace
}
else
{
// Better way is to set "document_root" option, put "index.html" file
// into document_root and return MG_FALSE here. We're printing HTML
// page by hands just to keep everything in one C file.
print_html_form
(
conn
);
}
return
MG_TRUE
;
}
else
if
(
ev
==
MG_AUTH
)
{
return
MG_TRUE
;
}
return
MG_FALSE
;
}
int
main
(
void
)
{
struct
mg_server
*
server
;
// Create and configure the server
server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
server
,
"listening_port"
,
"8000"
);
// Serve request. Hit Ctrl-C to terminate the program
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
mg_poll_server
(
server
,
1000
);
}
// Cleanup, and free server instance
mg_destroy_server
(
&
server
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment