Simple Contact Form in Clojure


#1

Hi everyone, how are you?

I am new to Clojure and I would like to know how to do simple stuff with it.

I was wondering if you could help me to understand how to program a simple contact form with validation with Clojure.

Fields: name, tel, e-mail, text

In PHP would be something like this:(mail.php)

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

HTML:

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

An example with Go.https://gowebexamples.com/forms/

Thanks.


#2

There are a lot of pieces behind a question like this. PHP is primarily a web-based language so there’s already the implicit assumption of a web server and a PHP page processor running behind the scenes – so the only “code” we see is the template for the web page that is rendered with a few variables substituted.

The Go version exposes more of the machinery: it uses an HTML template for the form itself and imports libraries to render that template and to serve the rendered HTML page (although it has no routing beyond hardcoding / and it has no form processing – but then nor has the PHP version).

In Clojure, the closest equivalent to the Go version is going to use Ring (with the embedded Jetty server) to serve HTTP requests and probably Selmer as the templating library.

For now, I’ll point you at https://github.com/framework-one/fw1-clj/tree/master/examples/usermanager which is a small, complete “contact form” application that combines Ring (for HTTP), Compojure (for routing), Component (for managing the start/stop lifecycle of the application and the HTTP server), Selmer (for templating), and clojure.java.jdbc for database persistence.

It does a lot more than the Go and PHP versions because it allows for contact listing, editing, addition, and deletion, and it persists all that into a local database. It also allows you to select your HTTP server at startup (between Jetty and http-kit).

It uses Boot – most Clojure examples use Leiningen – but you can see the various library dependencies and structure here: https://github.com/framework-one/fw1-clj/blob/master/build.boot (it includes more dependencies than the user manager example actually needs because it is also the basis for “Framework One” which is a small, convention-based MVC ‘framework’ that I wrote – before deciding that Clojure doesn’t need a framework :slight_smile: ).

I guess the answer to the basic question, as asked, is https://github.com/framework-one/fw1-clj/blob/master/examples/usermanager/views/user/form.html although this is a substantially fancier HTML page with div and label etc :slight_smile:


#3

Thanks Sean!!

I really appreciated.