Link to home
Start Free TrialLog in
Avatar of Jasbir21
Jasbir21

asked on

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
Avatar of VincentPuglia
VincentPuglia

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


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

<input type="checkbox" name="de" value="<%=rs.getString("username") %>">
id is local thats why it come undefined.
try to make it global and see if it works.
Avatar of Jasbir21

ASKER

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

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


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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ..

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,
  thanks it helps ..how to change it so that only one box can be checked because i need to send one id,


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

thanks
if u want to be simple just change checkbox to radio
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>
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
>> 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.
>>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