mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-19 08:03:14 +08:00
Add a radio button to teh settins_panel example
PUBLISHED_FROM=2daba7380508df24d909f922c5009286b4d8e7cf
This commit is contained in:
parent
79325bd219
commit
ced823cd95
@ -8,11 +8,12 @@
|
||||
struct device_settings {
|
||||
char setting1[100];
|
||||
char setting2[100];
|
||||
char setting3[4];
|
||||
};
|
||||
|
||||
static const char *s_http_port = "8000";
|
||||
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) {
|
||||
/* Get form variables and store settings values */
|
||||
@ -20,7 +21,11 @@ static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
||||
sizeof(s_settings.setting1));
|
||||
mg_get_http_var(&hm->body, "setting2", 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 */
|
||||
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);
|
||||
@ -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);
|
||||
} else if (strcmp(param, "setting2") == 0) {
|
||||
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 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>RESTful API demo</title>
|
||||
<title>ACMEtron 9000 - Settings</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<style type="text/css">
|
||||
* { outline: none; }
|
||||
@ -17,7 +17,7 @@
|
||||
}
|
||||
code { background: #eee; padding: 0 0.3em; border-radius: 0.2em; }
|
||||
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; }
|
||||
@media (max-width: 900px) {
|
||||
div.content { width: auto; margin: 2em; padding: 1em; }
|
||||
@ -36,6 +36,7 @@
|
||||
$(document).on('submit', '#settings_form', function() {
|
||||
var data = {};
|
||||
$('#settings_form [name]').each(function(index, el) {
|
||||
if ($(el).attr('type') == 'radio' && !$(el).is(':checked')) return;
|
||||
data[$(el).attr('name')] = $(el).val();
|
||||
});
|
||||
$('#result').text('');
|
||||
@ -91,6 +92,9 @@
|
||||
name="setting1" value="<!--#call setting1 -->" >
|
||||
<label>Setting 2:</label> <input type="text"
|
||||
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>
|
||||
|
||||
<div style="margin: 1em 0;">
|
||||
|
Loading…
Reference in New Issue
Block a user