formsscalah2playframework-2.4playframework-evolutions

Insert data in H2 using HTML form


I'm using Play! Framework 2.4. I can make a table and insert data via the evolution .sql scripts but how do I setup my Appication.scala, routes etc to make a form insert data?

PS I'm quite new to Play


Solution

  • The best way play fromework recommends is using helper and template system.

    https://www.playframework.com/documentation/2.4.x/ScalaForms

    But if you want to try with plain html and handler request in the controller by your own so you can try this:

    Application.scala

    def save = Action { implicit request =>
    //here handle params from html form, 
    //this will return Option[Map[String,ArrayBuffer]]                                                                     
    val optionMap = request.body.asFormUrlEncoded
    println(optionMap)
    //saving things here
    Ok("saved")
    

    }

    in plain html form

    <form action="/savedata" method="post" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded">
    <input type="text" name="name" />
    <input type="text" name="lastName" />
    <input type="submit" />         
    </form>   
    

    conf/routes

    POST   /savedata                        controllers.Application.save
    

    hope this helps. regards