1

I am new to both web application and the play framework, and the problem I am asking might be very naive. However, I googled around for a little while and couldn't find a good answer to it, so please bear with me.

First, my platform is play-2.1.1 + java 1.6 + OS X 10.8.3.

A short version of the problem: I have a form of submit button with action="hello?id=100". However, when I click the button, the request being sent seems to be hello? rather than hello?id=100. The action for this request expects the parameter id, so I get an error for hello?.

Here is the full setup.

conf/routes:

GET     /                           controllers.Application.index()
GET     /hello                      controllers.Application.hello(id: Long)

app/controllers/Application.java:

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render());
    }

    public static Result hello(Long id) {
        return ok("Hello, No. " + id);
    }

}

app/views/index.scala.html

This is an index page.

<form name="helloButton" action="hello?id=100" method="get">
    <input type="submit" value="Hello">
</form>

According to the play documentation, the id is supposed to be extracted from the query string ?id=100. However, when I click the submit button, the request becomes hello? rather than hello?id=100, and thus I get an error like this:

For request 'GET /hello?' [Missing parameter: id]

Can someone tell me why this is happening, please? Thank you in advance.

JBT
  • 8,498
  • 18
  • 65
  • 104
  • Does this answer your question? [When submitting a GET form, the query string is removed from the action URL](https://stackoverflow.com/questions/1116019/when-submitting-a-get-form-the-query-string-is-removed-from-the-action-url) – miken32 Jun 07 '21 at 22:46

1 Answers1

1

The problem is with the form:

<form name="helloButton" action="hello?id=100" method="get">
    <input type="submit" value="Hello">
</form>

As the form method is set to get, it's changing the query string. method="get" tells the browser to add the contents of the form to the query string which means the current query string gets removed.

You can add the ID as a hidden field in the form like this:

<form name="helloButton" action="hello" method="get">
    <input type="hidden" name="id" value="100">
    <input type="submit" value="Hello">
</form>

That will tell the browser to add the hidden field to the query string resulting in hello?id=100. Alternatively you could change the form method to POST.

Sam
  • 4,475
  • 2
  • 28
  • 31