I'm developing a web app for a reservation system and so far I achieved to send a mail with libcurl
and display the form in HTML using fcgi_stdio.h
, but both separately.
This is how it looks
#define __USE_XOPEN
#define _GNU_SOURCE
#include <stdio.h>
#include <fcgi_stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mail.h"
#include "booking.h"
int
main()
{
char to[20], name[20];
//char *year, *mon, *day, *hour, *min;
char date[20];
char num_guest[4], phone[10];
while (FCGI_Accept() >= 0) {
printf("Content-type: text/html\n\n");
printf("<h1>Welcome</h1>");
char *method = getenv("REQUEST_METHOD");
char form[2048] = {0};
char output[2048] = {0};
if(strcmp(method, "GET") == 0) {
strcpy(form,
"<form method='POST' action=''>"
"<input id='name' name='name' type='text'></input>"
"<br>"
"<input id='to' name='to' type='email'></input>"
"<br>"
"<input id='date' type='datetime-local' name='date' />"
"<br>"
"<input type='tel' "
"id='phone' name='phone' size='20' "
"minlenght='9' maxlenght='14' required>"
"<br>"
"<input type='number' id='num_guest' name='num_guest'"
"min='1' max='18'>"
"<br>"
"<input type='submit' value='Submit'>"
"</form>"
);
} else if (strcmp(method, "POST") == 0) {
int ilen = atoi(getenv("CONTENT_LENGTH"));
//char *bufp = malloc(ilen);
char *bufp = (char *) malloc(ilen * sizeof(char));
fread(bufp, ilen, 1, stdin);
printf("<h3>Thank you for your reservation!</h3>");
strcpy(output, bufp);
free(bufp);
/* declare data variables */
sscanf(output,
"name=%[^&]&to=%[^&]&date=%[^&]&phone=%[^&]&num_guest=%[^&]",
name, to, date, phone, num_guest);
replace(date, "%3A", ":"); // makes the date readable
replace(to, "%40", "@"); // makes the mail readable
}
printf(
"<div>"
"%s"
"</div>",
form
);
}
return 0;
}
The code above works fine, but I would like to call these functions after clicking the Submit
button.
book_for(to, name, num_guest, phone, date);
send_mail("addr", "confirmation.mail");
Those functions alone and declaring before the arguments as char *name = 'some name';
work fine too sending the mail to the recipient.
Thanks for your help :)