File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Javascript coding problem for two submit buttons in one form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript coding problem for two submit buttons in one form" Watch "Javascript coding problem for two submit buttons in one form" New topic
Author

Javascript coding problem for two submit buttons in one form

vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Hi, i have two submit buttons in one form. Each goes to a different jsp page. The script i have works fine, the problem is whatever values i entered into the form is now visible in the address bar for example

http://localhost:8080/test1.jsp?name=testing

How do i ensure that the values which i enter into the form is not shown in the address bar. This is my script.

<script language="JavaScript"><!--
function sendForm(action,encoding) {
if (document.images) {
document.registerForm.action = action;
document.registerForm.encoding = encoding;
}
return true;
}
//--></script>

<form name="registerForm">
<input type="text" name="myText" value="some text">
<input type="submit" value="Submit1" onclick="sendForm('test1.jsp','text/plain')">
<input type="submit" value="Submit2" onclick="sendForm('test2.jsp','text/plain')">
</FORM>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Set the form's method to POST (form method="POST"). It's GET by default.


[My Blog]
All roads lead to JavaRanch
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Thank you for your help Satou kurinosuke. But where do i Set the form's method to POST (form method="POST").
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Thank you Satou kurinosuke. But when i try it the values do not get passed onto to the database for storage. I'm using jsp and mysql.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by vanan saravanan:
Thank you Satou kurinosuke. But when i try it the values do not get passed onto to the database for storage. I'm using jsp and mysql.


First, get values from request parameters and print it on console or screen and see if you are getting the right values then perform DB operation.

Post your code if you can.

vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Thank you rathi ji for your help.

When i use the script without method="post" the values are saved into database with no problems. When i add method="post" into the script the database receives null value. Here is the script for my jsp page. The javascript is posted in the first post. Thank you all for your valuable help.

<%
Statement stm=null;
stm= con.createStatement();
String Childname = request.getParameter("name");
String insertChildname = "insert into ds (Childic) values ('"+Childname+"')";
stm.executeUpdate(insertChildname);
con.close();
%>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I don't recall talking about "post", but about "POST"
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Tried using "POST". Does not seem to work too. Database value is "null"
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by vanan saravanan:
Thank you rathi ji for your help.

When i use the script without method="post" the values are saved into database with no problems. When i add method="post" into the script the database receives null value. Here is the script for my jsp page. The javascript is posted in the first post. Thank you all for your valuable help.

<%
Statement stm=null;
stm= con.createStatement();
String Childname = request.getParameter("name");
String insertChildname = "insert into ds (Childic) values ('"+Childname+"')";
stm.executeUpdate(insertChildname);
con.close();
%>


... and the same code work for GET request but not for POST.

I am just guessing, close your statement before connection closing and did you check (print) the value before sending to DB?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Satou kurinosuke:
I don't recall talking about "post", but about "POST"


Satou, I think, HTML is not case sensitive then why this... :roll:
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
how do i (print) the value before sending to DB...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Satou, I think, HTML is not case sensitive then why this...

Because I'm a fool and I don't like smallcase POST
Sorry for the useless post (<-- this post is lowercase, ok )
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by vanan saravanan:
how do i (print) the value before sending to DB...


out.println() should do.
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Got the problem solved, thank you to all who helped.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

if you could bring some light on what the problem was
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
I changed the script to something else. Its easier and it works. The script as follows. Thank you once again to all who helped.


<input name="b1" type="button" value="submit1"
onclick ="this.form.action ='test1.jsp';this.form.submit()">

<input name="b2" type="button" value="submit2"
onclick="this.form.action ='test2.jsp';this.form.submit()">
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javascript coding problem for two submit buttons in one form