Parsing values from multiple checkboxes using compojure

698 views Asked by At

I have created small compojure web application, which can display multiple values, fetched from other website, using provided URL. At the moment, this URL is hard coded in one of my functions, and now I would like to add feature for dynamical URL creation, based upon values in text field and checkbox.

This is how my page looks like:

(defn view-layout [& content]
  (html [:body content]))

(defn view-input []
    (view-layout
     [:h2 "Find"]
     [:form {:method "post" :action "/"}
     ( for [category ["Cat1" "Cat2" "Cat3"]]
      [:input {:type "checkbox" :id category } category ] )       
      [:br]
     [:input {:type "text" :id "a" :value "insert manga name"}] [:br]
     [:input.action {:type "submit" :value "Find"}]
     [:a {:href "/downloads"} "Downloads"]]))

(defn view-output []
  (view-layout
    [:h2 "default images"]
    [:form {:method "post" :action "/"}         
      (for [name (get-content-from-url (create-url))]        
        [:label name [:br]]            
           )]))

(defn create-manga-url
  []
  "http://www.mysite.net/search/?tfield=&check=000")

Here are the routes:

(defroutes main-routes            
   (GET "/" []
      (view-input))
  (GET "/downloads" []
      (view-downloads))
  (POST "/" []
       (view-output) ))

At the moment, I need help with (create-url) function (returns a string), where I would like to fetch all fields, mandatory for my search (one text field and 3 checkboxe's) , and parse values from them, which will be fed into (concatenated) the URL - for checkbox, if checked, the check section will have value 1, instead of 0, or remain 0 if not (check=100, or 010, 011 if two check boxes were selected) . In case of text field, the tfield=userinputtext.

EDIT I spent a lot of time as a .Net and Java developer, and this part of compojure is a total mystery for me. This is what I would like to achieve with (create-url) function (pseudo code written in OO style):

 (defn create-url [*text_field cbox1 cbox2 cbox3*]
(def url "http://www.mysite.net/search/?")
(def tfield "tfield=")
(def cbox "&check=")

(if (checked? cbox1)
(str cbox "1")
(str cbox "0"))

(if (checked? cbox2)
(str cbox "1")
(str cbox "0"))

(if (checked? cbox3)
(str cbox "1")
(str cbox "0"))

(str tfield (:value text_field))

(str url tbox cbox))

I apologize for how this pseudo code looks like, but this is the part that I would like to learn: How can I scoop data from form, and parse it (in this case i would like to attachh values from form fields into the string)

Can anyone help me with this?

1

There are 1 answers

0
Terje Sten Bjerkseth On

First, you need to add 'name' attributes to your HTML input elements. The 'id' attributes aren't sent to the server on post.

Next, I guess a quick way of doing this similar to your example is:

(POST "/" [a Cat1 Cat2 Cat3] (create-url a [Cat1 Cat2 Cat3]))

and then something like this:

(defn checked? [c]
  (and c (= c "on")))

(defn checked->num [c]
  (if (checked? c) "1" "0"))

(defn create-url [a cats]
  (str "x?tfield=" a "&check="
    (apply str (for [c cats] (checked->num c)))))

Or just drop the two helpers:

(defn create-url [a cats]
  (str "x?tfield=" a "&check="
     (apply str (map #(if (= "on" %) "1" "0") cats))))