• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Why is javascript function called even before onClick?

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following simple code.It is showing no object error a common in Javascipt But the Fn that I intend to be called after onCLick is invoked directly when I run the servlet.The servlet just contains Login and Password fields.I wished to have both SUBMIT and CANCEL buttons in the same row.So instead of submitting form by using input type=SUBMIT I m using input type=BUTTON and submitting the form by onClick event.
One more Q.
Can I call two events separated by commas by clicking the same button once?
HERE IS THE CODE ------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Administrator extends HttpServlet
{
public void init(ServletConfig config)throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<meta http-equiv=Content-Type"+
" "+"content=text/html; charset=iso-8859-1>");
out.println("<meta name=GENERATOR content=Microsoft FrontPage Express 2.0>");
out.println("<title>AdministratorLogin.html</title>");
out.println("<script language=javascript>");
out.println("<!--");
out.println("function checkforblanks(){if(document.myform.loginname.value==''){alert('Please'+' '+'enter Login Name.');return'+' '+'false;}else{if(document.myform.password.value==''){alert('Please enter password.');return'+' '+'false;}}return true;}");
out.println("-->");
out.println("</script>");
out.println("</head>");
out.println("<body bgcolor=#00FFFF>");
out.println("<p align=center><font color=#0000FF"+ " "+"size=6><strong>Administrator"+
" "+"Login </strong></font></p>");
out.println("<p> </p>");

out.println(" <div align=center><center>");
out.println(" <table border=0 width=75%>");
out.println(" <form name=myform method=post>");
out.println(" <tr>");
out.println(" <td><strong>Login Name</strong><font"+
" "+"color=#FF0080><strong>*</strong></font></td>");
out.println(" <td><strong>:</strong></td>");
out.println(" <td><input type=text size=20 maxlength=20"+
" "+"name=loginname></td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=33%><strong>Password</strong><font"+
" "+"color=#FF0080><strong>*</strong></font></td>");
out.println(" <td width=33%><strong>:</strong></td>");
out.println(" <td width=34%><input type=password size=20"+
" "+"maxlength=20 name=password></td>");
out.println(" </tr>");
out.println(" </table>");
out.println("</center></div>");
out.println("<p align=center> </p>");
out.println("<div align=center><center>");
out.println("<table border=0>");
out.println(" <tr>");
out.println("<td><input type=button value=Submit onClick=return"+" "+
"checkforblanks()></td>");
out.println("<td><input type=reset value=Cancel></td>");
out.println(" </tr>");
out.println("</form>");
out.println("</table>");
out.println("</center>");
out.println("</div>");
out.println("</body>");
out.println("</html>");
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm moving this thread to the Servlets and JSP's forum because I think you will get a better response over there.
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
out.println("<td><input type=button value=Submit onClick=return"+" "+
"checkforblanks()></td>");
did you try:
out.println("<td><input type=button value=Submit onClick='return"+" "+
"checkforblanks()'></td>");
instead? I put 'return checkforblanks()' in a single quotes.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
out.println("function checkforblanks(){
if(document.myform.loginname.value==''){
alert('Please'+' '+'enter Login Name.');
return;
}else if(document.myform.password.value==''){
alert('Please enter password.');return;
}//code reaches here means fields are not empty so
document.myform.submit();
");
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see anything wrong in your code except what Yuriy and Kumar pointed out here. But what i'm wondering is that even with those errors you might have got messages "error in page" or no call to your javascript function but not "no object found". I got an error like these sometime ago and it worked for me when i placed my javascript after the html </FORM> statement(so as to make sure the "document.myform.loginname.value" reference is made after the actual form declaration). Try that ,may be it helps

Ajan
[This message has been edited by Ajan Balakrishnan (edited February 04, 2001).]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vaibhav,
Can you try using the onSubmit in the form tag.?
<form name="xx" method="post" action="xxx" obSubmit="return checkforblanks(document.xx)">{
and in the javascript call mention
funtion checkforblanks(FormObj){
}
try this out and check the results...
bye
Joseph
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys I ll try it.I think joseph's code seems interesting .Generally either we use onClick for buttons.I ll try using it in Form tag.but anyhow my code seems to be working now.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic