Solved

why undefined -?urgent

Posted on 2003-11-24
29
746 Views
Last Modified: 2012-06-22
hi,

I have a checkbox like this:
<input type="checkbox" name="de" value="rs.getString("username") >
<input type=button value="Delete"  onclick="Delete(this.form)">







     
..and javascript function is like this:
   
   function Delete(theForm){
       
 var id=theForm.de.value


 pw = window.open("deleteuser.jsp?id="+id,"newProject",
            "scrollbars=yes,status=no,resizable=yes,height=200,width=400");
      pw.focus();


   }


when i check the checbox and click Delete i get the value of id undefined, why is that, is there anything wrong with code.

Thanks
0
Comment
Question by:Jasbir21
  • 10
  • 5
  • 5
  • +3
29 Comments
 
LVL 15

Expert Comment

by:VincentPuglia
Comment Utility
Hi,

  Can't really say with code you posted, but the best guesses I can make, especially since you do not say where you get the error (before the window is opened or after):

var id=theForm.de.value

Since theForm is the form object, I have to presume that the problem lies in the variable name.  place an alert after the assignment and see what you get:

var id=theForm.de.value
alert(id)

If that is where the error is, you need to change the name of the var 'id' to something like 'theID' since 'id' is probably being confused with the reserved word.

If none of the above helps, post some more code

Vinny


0
 
LVL 15

Expert Comment

by:VincentPuglia
Comment Utility
Hi,

  You do have form tags around the checkboxes,right? And you are not getting the error from the popup?  If you are, try appending 'opener.' to whatever code you are using to access the field.

Vinny
0
 
LVL 5

Expert Comment

by:ho_alan
Comment Utility
is that sth. u want?
undefine should be due to this.

<input type="checkbox" name="de" value="<%=rs.getString("username") %>">
0
 
LVL 24

Expert Comment

by:shivsa
Comment Utility
id is local thats why it come undefined.
try to make it global and see if it works.
0
 

Author Comment

by:Jasbir21
Comment Utility
sorry, just saw the input, pls give me a few minutes to read and then i would post the whole code..

thanks
0
 
LVL 15

Expert Comment

by:VincentPuglia
Comment Utility
shivsa:  local or not makes no difference; the function has the form object, so it can grab the field value.

ho_alan: good catch

Vinny
0
 

Author Comment

by:Jasbir21
Comment Utility
hi,
  when i put alert, i get undefined,

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">


<!--To delete information
     
   function Delete(theForm){
       
  var theID=theForm.de.value
alert(theID);

 pw = window.open("deleteuser.jsp?id="+id,"newProject",
            "scrollbars=yes,status=no,resizable=yes,height=200,width=400");
      pw.focus();


   }
         

//-->


</script>

the jsp part is

.....
<%
    do

       {
  %>
       <tr>
       <td align=center class="data" ><input type="checkbox" name="de" value="<%=rs.getString("username")%>"></td>
     
       <td align=center class="data" ><%=rs.getString("name")%></td>

       <td align=center class="data" ><%= rs.getString( "username") %> </td>

<td align=center class="data" ><%= rs.getString( "skill") %> </td>
       
       
</tr>
       
   
  <%
         }  while( rs.next() ) ;
     
  }


  %>



but if i were to try with drop down list, the value is not undefined.

thanks
0
 
LVL 15

Expert Comment

by:VincentPuglia
Comment Utility
hi,

 where are the form tags? what does the rendered html look like?

="<%=rs.getString("username")%>"></td>
try putting single quotes around 'username' rather than double quotes

Vinny
0
 

Author Comment

by:Jasbir21
Comment Utility
hi,
   
   
 
<%@ page import="java.sql.*, com.dhal.*"%>





<html>
<head>



<title>Main Page</title>

<%
  //***Declaration of variables...
    String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
    Connection connection = null;
    Statement statement = null;
    ResultSet rs=null;
    String query="";
    String user="";
String e="";
    try
      {

     //***loading the jdbc driver...  
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     connection = DriverManager.getConnection(connectionURL, "", "");
     statement=connection.createStatement();
   


    }

   catch( SQLException ex ) { ex.printStackTrace() ; }
   catch( ClassNotFoundException ex ) {ex.printStackTrace() ;}
   
%>


<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">


<!--To delete information
     
   function Delete(theForm){
       
  var id=theForm.de.value
alert(id);

 pw = window.open("deleteuser.jsp?id="+id,"newProject",
            "scrollbars=yes,status=no,resizable=yes,height=200,width=400");
      pw.focus();


   }
         

//-->


</script>
<style>
<!--
table.header { margin-left:160px; width:60%; }
td.heading    { border:  thin solid #cccccc; background-color:#FFF8DC; padding:2px;  }
td.data        { border:thin solid #cccccc; background-color:#FFFFF0; padding:2px;  }
//-->
<!--
form  {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 15px;
      color: #000000;
      }

label {
      float: left;
      width:50px;
      clear:all;
      }
//-->

</style>
</head>
<body BGColor="#FFFFF0">



<form action="menuuser.jsp" name="form1">


</br>
 


<%

 


           query="select * from volunteer order by name";
           statement=connection.createStatement();
           rs=statement.executeQuery(query);

 if(rs.next()){

  %>

   <div style="overflow: auto; width: 60%; height: 300;  padding:0px; margin-left:160px; ">  
   <table class="header" border="1" >
   <tr>
     <td align=center class="heading">
      </td>
     
     <td align=center class="heading">
       Name
     </td>

   <td align=center class="heading">
     Username
   </td>
   
     
   <td align=center class="heading">
      Skill
   </td>


   </tr>

  <%
    do

       {
  %>
       <tr>
       <td align=center class="data" ><input type="checkbox" name="de" value="bad"></td>
     
       <td align=center class="data" ><%=rs.getString("name")%></td>

       <td align=center class="data" ><%= rs.getString( "username") %> </td>

<td align=center class="data" ><%= rs.getString( "skill") %> </td>
       
       
</tr>
       
   
  <%
         }  while( rs.next() ) ;
     
  }


  %>


 </table>
</div>






 
   
  <%
//***To ensure all the database connections are close
   
   try { if( rs != null ) rs.close(); rs=null ; } catch( Exception ex ) {ex.printStackTrace() ;}
   try { if( statement != null ) statement.close() ; statement=null; } catch( Exception ex ) {ex.printStackTrace() ;}
   try { if( connection != null ) connection.close() ; connection=null; } catch( Exception ex ) {ex.printStackTrace() ;}

   
  %>
 

<input type=submit value="Delete"  onclick="Delete(this.form)">



 
</form>
</body>
</html>  
 
I try with value='hello'

still i get undefined, is there a difference in assigning value to of type of checkbox, i mean if input type
<input type="text ..value='yy'>
but checkbox is there any difference.

thanks
 
0
 
LVL 5

Expert Comment

by:ho_alan
Comment Utility
Vinny is correct :-)

if you want to relate the <input> stuff form in JS functions
u have to embed it with <form name=theForm> and </form>


0
 
LVL 5

Expert Comment

by:ho_alan
Comment Utility
try to add "return true" at the end of the JS function
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
on this line:
     <input type="checkbox" name="de" value="rs.getString("username") >
it looks like you are mixing asp code and javascript code. That is no good.

if I am correct in thinking that  rs.getString("username")   should be performed by javascript, the line should be:

     <input type="checkbox" name="de" value="<% =rs.getString("username") %>">

regards JakobA
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
Oops:   'performed by javascript'   should be   'performed by ASP'
0
 
LVL 5

Expert Comment

by:ho_alan
Comment Utility
there should be no difference for getting the value
<input type=text> and <input type=checkbox>
check lower and upper case

Jakob : your point (<%=xxxxxx %>) is already spotted out in the comments before :-)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Jasbir21
Comment Utility
hi,
   i tried ,thanks.

I mean i did try to put explicitly value='bad', meaning the value is bad and

value='good', meaning value is good,

but it still seem to work,

i mean it seems like the input value is not getting anything

thanks
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
>>  ho_alan  >>  you are right, I had missed your earlier post. sorry
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
html forms only send a  name=value  pair for checkboxes that are checked, for uncehcked checkboxes you recieve nothing.
0
 

Author Comment

by:Jasbir21
Comment Utility
..i think i get it, that's why it works with drop down list and not checkbox, but lets say i still need the checkbox,

I mean when the menuuser.jsp appears, the checkbox is not be checked.

But when the user check the checkbox, the id needs to be sent,how is that then..

I mean what do i need to do not to get undefined then

thanks
0
 
LVL 5

Assisted Solution

by:ho_alan
ho_alan earned 50 total points
Comment Utility
u can use "theForm.de.checked"
if theForm.de.checked
      //delete the item, theForm.de.value
else
     //blah blah

here is an example:

<body>
<script>
function chk()
{
   if (document.fm1.chk1.checked)
         alert(document.fm1.chk1.value);
   return true;
}
</script>
<form name=fm1>
<input type=checkbox value=1 name=chk1>
<input type=button onclick="chk();">
</form>
</body>
0
 

Author Comment

by:Jasbir21
Comment Utility
hi,

 this is what i did:

function Delete(theForm){
if(!theForm.po.checked){

   alert(theForm.po.value);
return true;
}


         }


Surprisingly, the alert did pop up with undefined even when i checked the box, what do i do, i really don't know what to do ..

0
 
LVL 3

Accepted Solution

by:
etain earned 90 total points
Comment Utility
Hope this help
<script>
function findObj(obj_id, win)
{
      var doc;
      if (win == null || win == "undefined") doc = document;
      else
      {
            doc = win.document;
      }
      if(document.all)
      {
            return(doc.all[obj_id]);
      }
      
       else if(document.getElementById)
      {
            return(doc.getElementById(obj_id));
      }
      
      else if(document.layers)
      {
            return(getObj(obj_id, doc));
      }
}

function getObj(obj_id, d)
{
      var doc = d.layers[obj_id];
      for(var i=0 ; i < d.layers.length && doc==null ; i++)
            doc = getObj(obj_id, d.layers[i].document);
      return doc;
}

   
function Delete()
{
 var obj = findObj("de")
 var id = ""
 if (obj != null)
 {
       if (obj.length > 1 )
      {
            for (c=0; c < obj.length;c ++)
            {
                  if (findObj("de")[c].checked)
                  {
                        id = id + findObj("de")[c].value
                  }
            }
      }
      else
      {
            if (findObj("de").checked)
            {
                  id = id + findObj("de").value
            }
      }
}      
 alert(id)

/* pw = window.open("deleteuser.jsp?id="+id,"newProject",
            "scrollbars=yes,status=no,resizable=yes,height=200,width=400");
      pw.focus();*/
 }
</script>
<body>
   <input type="checkbox" id ="de" name="de" value="1" >
   <input type="checkbox" id ="de" name="de" value="2" >
   <input type=button value="Delete"  onClick="Delete()">
</body>
0
 

Author Comment

by:Jasbir21
Comment Utility
hi,
  thanks it helps ..how to change it so that only one box can be checked because i need to send one id,


0
 

Author Comment

by:Jasbir21
Comment Utility
i mean if there is for example 10 checkbox, a user click one ,

thanks
0
 
LVL 3

Expert Comment

by:etain
Comment Utility
if u want to be simple just change checkbox to radio
0
 
LVL 3

Expert Comment

by:etain
Comment Utility
using checkbox add this function

 function Select(sobj)
 {
       if (sobj.checked)
      {
            var obj = findObj("de")
            if (obj != null)
            {
                   if (obj.length > 1 )
                  {
                        for (c=0; c < obj.length;c ++)
                        {
                              if(findObj("de")[c] != sobj ) findObj("de")[c].checked = false
                        }
                  }
            }
      }
 }
</script>
<body>
   <input type="checkbox" id ="de" name="de" value="1" onClick="Select(this)">
   <input type="checkbox" id ="de" name="de" value="2" onClick="Select(this)">
   <input type="checkbox" id ="de" name="de" value="3" onClick="Select(this)">
   <input type=button value="Delete"  onClick="Delete()">
</body>
0
 

Author Comment

by:Jasbir21
Comment Utility
hi,
  I think you are right, actually i want to use checkbox that allows me to send few id, actually i need some changes on the same code,

I am closing this question and posting a new one.Pls look at the link that i would place.
Etain, the modications are on your code.Could you pls see the link.
I have asked a lot here.

I would like to thank everyone for helping me..

thanks
0
 

Author Comment

by:Jasbir21
Comment Utility
The modification for the code:
is at the link

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20807928.html

thanks
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
>> etain >>
  in your example of  Date: 11/25/2003 12:47AM PST you have several checkboxes with the same name and id. this is naughty.  identical name values are used only for grouping radio buttons. id-values should never be identical.
0
 
LVL 3

Expert Comment

by:etain
Comment Utility
>>JakobA>>
In HTML, if id is identical it become array.
There is no rule saying that u cant group HTML element, other then radio buttons
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now