The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Beginner form question
scorks
post Jan 29 2015, 11:04 PM
Post #1





Group: Members
Posts: 7
Joined: 16-January 15
Member No.: 22,040



Hi there, I'm very new to forms, and am looking to make a counter that when the "increase" button is pressed, the counter is increased by 1 and displayed. If the decrease button is pressed, the counter is decreased by one and displayed. Also, there is a text box where the user can enter a new counter number. I need to figure out a way to save my counter variable so that when the button is pressed, it remembers what the previous number stored was. So far I have:

CODE

import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.port;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class exer7 {

    private static int counter = 0;
    
    private static String FORM_PAGE =
        "<html>\n" +
        "<body>\n" +
        "<form action='/counter' method='POST'>\n" +
        "<label><input type='text' name='countNum'></label>\n" +
        "<input type='submit' name='count' value='Increase'>\n" +
        "<input type='submit' name='count' value='Decrease'>\n" +
        "The current count is: " +
        counter +
        
        "</body>\n";

        public static void main(String[] args) {
    
        get("/counter", (request, response) -> FORM_PAGE );

        post("/counter", (request, response) -> {
            String c = request.queryParams("count");
            
           int amt = Integer.parseInt(request.queryParams("countNum"));
           if (c.equals("Increase")) {
               counter = amt+1;
           }
           else if (c.equals("Decrease")) {
              counter = amt-1;
          }
          else {
              // return an error page
          }    

    return Integer.toString(counter);    

        });
    
    }
}



It's not 100% complete or correct yet, but could anyone help out with the problem at hand? I'd really appreciate it! Thanks!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
scorks
post Jan 29 2015, 11:05 PM
Post #2





Group: Members
Posts: 7
Joined: 16-January 15
Member No.: 22,040



Also, please ignore my random imports. They're from my previous question.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 29 2015, 11:40 PM
Post #3


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Are you trying to do this with server or client side code? Or are you trying to use Java? (I don't do Java) A javascript counter (client side) would only work if the user has not disabled javascript. Server side code would need to use AJAX and you would need a place to store the counter value.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 29 2015, 11:45 PM
Post #4


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Oh, I think HTML5 provides a counter. See this link .
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 30 2015, 09:05 AM
Post #5


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Doesn't look well supported though. Yeah, what language are you using?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th April 2024 - 08:23 PM