Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

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
0
Jasbir21
Asked:
Jasbir21
  • 10
  • 5
  • 5
  • +3
2 Solutions
 
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


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

<input type="checkbox" name="de" value="<%=rs.getString("username") %>">
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

thanks
0
 
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
0
 
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
0
 
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
0
 
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
 
0
 
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>


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

0
 
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>
0
 
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,


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

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

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

thanks
0
 
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.
0
 
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 10
  • 5
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now