0

hi,i would like to make a simple hotel.Firstly,I created a simple form(with no validations etc) but i don't know how when i press Submit button then this details to save in a database using Mysql. Can anyone tell me how to do this this?
<br>

<%--
    Document   : StartPage
    Created on : 14-Nov-2010, 16:07:29
    Author     : theo
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<script>
    function onClick()
      {
      return true;
      }
  </script>
  <html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hotel Details</h1>
         <form name="form1" action="Details.jsp" method="post" onsubmit="javascript:return onClick()" >
          Rooms
          <br>
         <select id="Origin" name="Origin" onchange = "originChanged()">

  <option>Please Choose</option>
  <option>Bed and Breakfast</option>
  <option>Room</option>
    </select>
          </br>

          <h2></h2>
         Number of people
         <br>
    <select id="Origin" name="Origin">
    <option>Please Choose</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select>
         </br>
          <h3></h3>
         Number of rooms
    <br>
         <select id="Origin" name="Origin">
  <option>Please Choose</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
   </select>
    </br>
     <h4></h4>
         Type of room
<br>
         <select id="Origin" name="Origin" onchange = "originChanged()">

  <option>Please Choose</option>
  <option>Single</option>
  <option>Double</option>
  <option>Luxury double</option>
  </select>
        </br>
 <h5></h5>
         Breakfast
 <br>
<select id="Origin" name="Origin">
  <option>Please Choose</option>
  <option>Yes</option>
  <option>No</option>
   </select>
 </br>
 <br>
 <h5></h5>
         Start Date
    <select id="Origin" name="Origin">
    <option>Please Choose</option>
    <option>1</option>
    </select>
    <select id="Origin" name="Origin">
    <option>Please Choose</option>
    <option>December 2010</option>
    <option>January 2011</option>
    <option>February 2010</option>
    <option>March 2010</option>
    <option>April 2010</option>
    <option>May 2010</option>
    </select>
  </br>
   <h5></h5>
         Until
    <select id="Origin" name="Origin">
    <option>Please Choose</option>
    <option>1</option>
    </select>
    <select id="Origin" name="Origin">
    <option>Please Choose</option>
    <option>December 2010</option>
    <option>January 2011</option>
    <option>February 2010</option>
    <option>March 2010</option>
    <option>April 2010</option>
    <option>May 2010</option>
    </select>
  </br>
	</p>
	<p><input id=submit1 type=submit value=Submit name=submit1>
	</p>
  <form name="form1" action="PageTwo.jsp" method="post">





         </form>
    </body>
</html>

</br>

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by javaAddict
1

First of all.
You don't need this:

onsubmit="javascript:return onClick()"

The javascript: is used only if want to execute javascript when you click a link. For simple events use:

onsubmit="return onClick()"

Second. You have everything in your first form, but the button that submits it is in another form. You have this:

<form name="form1" action="Details.jsp" method="post" onsubmit="javascript:return onClick()" >

....
  <input id=submit1 type=submit value=Submit name=submit1>

<form name="form1" action="PageTwo.jsp" method="post">
</form>

You open a form tag, you don't close it and inside it you open-close another.
You should have everything you want to submit in ONE form:

<form name="form1" action="Details.jsp" method="post" onsubmit="return onClick()" >

....
  <input id=submit1 type=submit value=Submit name=submit1>

</form>

You can multiple forms if necessary but you must open-close them and each must have one submit button with its own data:

<form name="form1" action="page1.jsp">
  ... input tags ...
  <input type="submit" name="subm1" value="Submit 1">
</form>

<form name="form2" action="page2.jsp">
  ... input tags ...
  <input type="submit" name="subm2" value="Submit 2">
</form>

When you click submit1 then the first form will be submitted and if you click the submit2 the second form will be submitted.
But you need only one. So keep your code and have one form that opens/closes with one submit button.

Now after you submit to another jsp or servlet, you can take the values entered like this:
Put this code to the jsp or servlet that you submitted to.

String origin = request.getParameter("Origin"); // as argument use the name of the tag

Also in your code you have all of your inputs to have the same name. It is best for separate inputs to have different names. Also those selects only display the 1,2,3,4. You haven't assigned any values to them. If you submit them you will get nothing. You need to set what value you want each option to have. You have only set what would be displayed. Your selects should be like this:

Number of people
<br>
<select name="numOfPeople">
<option value="">Please Choose</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</br>
<h3></h3>
Number of rooms
<br>
<select name="numOfRooms">
<option value="">Please Choose</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option>4</option>
</select>

You can have different values than what is displayed:

<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>

You will choose May, but submit the value "5"

And use that "name" to get the value.

Now create in a separate class a method that connects to the database runs the query and inserts the arguments that you pass to it, in the database

After you have those values at the jsp/servlet you submitted, call the above method.

For further information look at the tutorial on top of this forum with title JSP Database Connectivity.

You will also need to do a lot of studying about HTML. There where many errors so before you proceed, better learn how to write proper html. Leave the database alone for now. Just try to write correct html code and submit the values to another page. After you get the values try to print them and see if you have done it correctly. Look at: http://www.w3schools.com/default.asp

Votes + Comments
Good reply
0

thank you very much for your help ans sorry about my errors....i did not check my code when i sent it here and i had many simple errors.
Can i ask you some more general questions.
1)i would like in a drop down list to have all the dates of the month.(1-31)
the easiest way to do this is

<br>
 <h1></h1>
         Start Date
    <select id="StartDate" name="StartDate" onchange ="DayChanged()">
    <option>1</option>
    <option>2</option>
             .
             .
             .
    <option>31</option>
  </select>
  </br>

this code has many lines and is not very useful.Is there any other way?

2)if i have 2 drop down lists again with dates(e.g departure-arrival) and i want a validation that if the period between these 2 is bigger than 14 days then 'error',is there any way to do this?

Thank you!!!

Edited by theo23: n/a

0

thank you very much for your help ans sorry about my errors....i did not check my code when i sent it here and i had many simple errors.
Can i ask you some more general questions.
1)i would like in a drop down list to have all the dates of the month.(1-31)
the easiest way to do this is

<br>
 <h1></h1>
         Start Date
    <select id="StartDate" name="StartDate" onchange ="DayChanged()">
    <option>1</option>
    <option>2</option>
             .
             .
             .
    <option>31</option>
  </select>
  </br>

this code has many lines and is not very useful.Is there any other way?

2)if i have 2 drop down lists again with dates(e.g departure-arrival) and i want a validation that if the period between these 2 is bigger than 14 days then 'error',is there any way to do this?

Thank you!!!

For the dates you should use peter_budo suggestion. But if you want to make select drop down list with little code you can try this:

<select name="month">
<%
for (int i=1;i<=12;i++) {
%>
  <option value="<%=i%>"><%=i%></option>
<%
}
%>
</select>

The same works if you want multiple <tr> lines at a table. In case you want to print a java array, you for loop it and in the for loop you put: <tr><td><%=array[i]%></td></tr> for example

For validation check out the javascript tutorials I sent. In a javascript function try to get the selected values of the the drop down lists, then compare them. If the validation is ok, submit the form. You can have a type="button" instead of a type="submit" and an onclick event.

<input type="button" value="Submit the Form" onclick="validate();" />

You will have to write the function validate and inside it use javascript code that submits the form: document.formName.submit(); Where formName is the name of your form

All the above can be found at the tutorials I gave you. Look at the HTML, javascript and DOM tutorials. You will also need to study JSP for using scriplets.

Edited by javaAddict: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.