Commit ced823cd authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Add a radio button to teh settins_panel example

PUBLISHED_FROM=2daba7380508df24d909f922c5009286b4d8e7cf
parent 79325bd2
...@@ -8,11 +8,12 @@ ...@@ -8,11 +8,12 @@
struct device_settings { struct device_settings {
char setting1[100]; char setting1[100];
char setting2[100]; char setting2[100];
char setting3[4];
}; };
static const char *s_http_port = "8000"; static const char *s_http_port = "8000";
static struct mg_serve_http_opts s_http_server_opts; static struct mg_serve_http_opts s_http_server_opts;
static struct device_settings s_settings = {"value1", "value2"}; static struct device_settings s_settings = {"value1", "value2", ""};
static void handle_save(struct mg_connection *nc, struct http_message *hm) { static void handle_save(struct mg_connection *nc, struct http_message *hm) {
/* Get form variables and store settings values */ /* Get form variables and store settings values */
...@@ -20,7 +21,11 @@ static void handle_save(struct mg_connection *nc, struct http_message *hm) { ...@@ -20,7 +21,11 @@ static void handle_save(struct mg_connection *nc, struct http_message *hm) {
sizeof(s_settings.setting1)); sizeof(s_settings.setting1));
mg_get_http_var(&hm->body, "setting2", s_settings.setting2, mg_get_http_var(&hm->body, "setting2", s_settings.setting2,
sizeof(s_settings.setting2)); sizeof(s_settings.setting2));
mg_get_http_var(&hm->body, "setting3", s_settings.setting3,
sizeof(s_settings.setting3));
printf("Settings updated to '%s' '%s' '%s'\n", s_settings.setting1,
s_settings.setting2, s_settings.setting3);
/* Send response */ /* Send response */
mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%.*s", mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%.*s",
(unsigned long) hm->body.len, (int) hm->body.len, hm->body.p); (unsigned long) hm->body.len, (int) hm->body.len, hm->body.p);
...@@ -31,6 +36,10 @@ static void handle_ssi_call(struct mg_connection *nc, const char *param) { ...@@ -31,6 +36,10 @@ static void handle_ssi_call(struct mg_connection *nc, const char *param) {
mg_printf_html_escape(nc, "%s", s_settings.setting1); mg_printf_html_escape(nc, "%s", s_settings.setting1);
} else if (strcmp(param, "setting2") == 0) { } else if (strcmp(param, "setting2") == 0) {
mg_printf_html_escape(nc, "%s", s_settings.setting2); mg_printf_html_escape(nc, "%s", s_settings.setting2);
} else if (strcmp(param, "setting3_is_one") == 0) {
if (strcmp(s_settings.setting3, "one") == 0) mg_printf(nc, "checked");
} else if (strcmp(param, "setting3_is_two") == 0) {
if (strcmp(s_settings.setting3, "two") == 0) mg_printf(nc, "checked");
} }
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>RESTful API demo</title> <title>ACMEtron 9000 - Settings</title>
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css"> <style type="text/css">
* { outline: none; } * { outline: none; }
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
} }
code { background: #eee; padding: 0 0.3em; border-radius: 0.2em; } code { background: #eee; padding: 0 0.3em; border-radius: 0.2em; }
label { display: inline-block; min-width: 5em; } label { display: inline-block; min-width: 5em; }
input { border: 1px solid #ccc; padding: 0.2em; margin-right: 2em; } input[type="text"] { border: 1px solid #ccc; padding: 0.2em; margin-right: 2em; }
a:link, a:visited { color: #69c; text-decoration: none; } a:link, a:visited { color: #69c; text-decoration: none; }
@media (max-width: 900px) { @media (max-width: 900px) {
div.content { width: auto; margin: 2em; padding: 1em; } div.content { width: auto; margin: 2em; padding: 1em; }
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
$(document).on('submit', '#settings_form', function() { $(document).on('submit', '#settings_form', function() {
var data = {}; var data = {};
$('#settings_form [name]').each(function(index, el) { $('#settings_form [name]').each(function(index, el) {
if ($(el).attr('type') == 'radio' && !$(el).is(':checked')) return;
data[$(el).attr('name')] = $(el).val(); data[$(el).attr('name')] = $(el).val();
}); });
$('#result').text(''); $('#result').text('');
...@@ -91,6 +92,9 @@ ...@@ -91,6 +92,9 @@
name="setting1" value="<!--#call setting1 -->" > name="setting1" value="<!--#call setting1 -->" >
<label>Setting 2:</label> <input type="text" <label>Setting 2:</label> <input type="text"
name="setting2" value="<!--#call setting2 -->" > name="setting2" value="<!--#call setting2 -->" >
<label>Setting 3:</label>
<input type="radio" name="setting3" value="one" <!--#call setting3_is_one --> > One
<input type="radio" name="setting3" value="two" <!--#call setting3_is_two --> > Two
</fieldset> </fieldset>
<div style="margin: 1em 0;"> <div style="margin: 1em 0;">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment