why undefined -?urgent

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
Jasbir21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VincentPugliaCommented:
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


VincentPugliaCommented:
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
ho_alanCommented:
is that sth. u want?
undefine should be due to this.

<input type="checkbox" name="de" value="<%=rs.getString("username") %>">
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

shivsaCommented:
id is local thats why it come undefined.
try to make it global and see if it works.
Jasbir21Author Commented:
sorry, just saw the input, pls give me a few minutes to read and then i would post the whole code..

thanks
VincentPugliaCommented:
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
Jasbir21Author Commented:
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
VincentPugliaCommented:
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
Jasbir21Author Commented:
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
 
ho_alanCommented:
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>


ho_alanCommented:
try to add "return true" at the end of the JS function
JakobACommented:
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
JakobACommented:
Oops:   'performed by javascript'   should be   'performed by ASP'
ho_alanCommented:
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 :-)
Jasbir21Author Commented:
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
JakobACommented:
>>  ho_alan  >>  you are right, I had missed your earlier post. sorry
JakobACommented:
html forms only send a  name=value  pair for checkboxes that are checked, for uncehcked checkboxes you recieve nothing.
Jasbir21Author Commented:
..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
ho_alanCommented:
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>
Jasbir21Author Commented:
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 ..

etainCommented:
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>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jasbir21Author Commented:
hi,
  thanks it helps ..how to change it so that only one box can be checked because i need to send one id,


Jasbir21Author Commented:
i mean if there is for example 10 checkbox, a user click one ,

thanks
etainCommented:
if u want to be simple just change checkbox to radio
etainCommented:
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>
Jasbir21Author Commented:
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
Jasbir21Author Commented:
The modification for the code:
is at the link

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

thanks
JakobACommented:
>> 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.
etainCommented:
>>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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.