Results 1 to 19 of 19

Thread: === Resolved ====Check box in Javascript

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276

    === Resolved ====Check box in Javascript

    I have a form with all records get from SQL database. Each record has a check box. If user login is 1 so they can select one or more checkboxes to delete orders. Otherwise, userlogin is 0 and they don't have permission to delete it. The userlogin is gotten from previous page and I use hidden field to store value. My Javascript code doesn't work. It didn't prompt me if I login as 0. The error at first if statement.
    ==================
    <script language ="JavaScript">
    function chkflds()
    {
    if(document.frmOrderDelete.CheckUser.value = '1')
    {
    for(var i=0;i<document.frmOrderDelete.CheckUser.length;i++)
    {
    if(document.frmOrderDelete.CheckBoxDelete[i].checked)
    {
    var confdel = confirm("Are you sure you want to delete the selected documents?")
    if( confdel)
    {
    document.frmDelete.action = "DeleteOrder.asp";
    ocument.frmDelete.submit();
    return true;
    } // end if
    } // end if checkbox checked
    else
    {
    alert("No documents selected for deletion!");
    return false;
    } // end else
    }//end for loop
    } //end if CheckUser
    else
    {
    alert("You don't have permission to delete order");
    return false;
    }
    }
    </script>
    <HTML>
    <form method="POST" name= "frmOrderDelele" ONSUBMIT="return chkflds()" >
    <% If sLogin = "Super" Then %>
    <input type="hidden" name="CheckUser" value="1" >
    <% Else %>
    <input type="hidden" name="CheckUser" value="0" >
    <% End If %>
    Last edited by learnervb; Jan 20th, 2003 at 05:08 PM.
    Tiny Mickey

  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Location
    Pennsylvania, USA
    Posts
    168
    if(document.frmOrderDelete.CheckUser.value = '1')

    should be

    if(document.frmOrderDelete.CheckUser.value == 1)
    Wydok

    "It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in 5 years."

    -John Von Neumann ca. 1949

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276
    I tried it before but it still gets error. The error say "document.frmOrderDelete.CheckUser is null or not an object.
    Tiny Mickey

  4. #4
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    I think you are not really need taht check box at all. Just pass sLogin variable to your function and check it there.
    Code:
    <script language ="JavaScript"> 
    function chkflds(strLogin) 
    { 
    if(strLogin=="Super") 
    { 
    for(var i=0;i<document.frmOrderDelete.CheckUser.length;i++) 
    { 
    if(document.frmOrderDelete.CheckBoxDelete[i].checked) 
    { 
    var confdel = confirm("Are you sure you want to delete the selected documents?") 
    if( confdel) 
    { 
    document.frmDelete.action = "DeleteOrder.asp"; 
    ocument.frmDelete.submit(); 
    return true; 
    } // end if 
    } // end if checkbox checked 
    else 
    { 
    alert("No documents selected for deletion!"); 
    return false; 
    } // end else 
    }//end for loop 
    } //end if CheckUser 
    else 
    { 
    alert("You don't have permission to delete order"); 
    return false; 
    } 
    } 
    </script> 
    <HTML> 
    <form method="POST" name= "frmOrderDelele" ONSUBMIT="return chkflds('<%=sLogin%>')" >

  5. #5
    Addicted Member
    Join Date
    Aug 2000
    Location
    Pennsylvania, USA
    Posts
    168
    It sounds like the hidden field CheckUser is not on the html page, or at least not within the form. Try:

    document.getElementById("CheckUser").value (if CheckUser has an id and not just name) or
    document.all.item("CheckUser").value

    ?

    Unforunately, I'm not that great with cross-browser Javascript since we only use IE at work
    Wydok

    "It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in 5 years."

    -John Von Neumann ca. 1949

  6. #6
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Originally posted by Wydok
    Try:

    document.getElementById("CheckUser").value (if CheckUser has an id and not just name)
    You could add id="CheckUser" or
    If it's by name: document.getElementsByName("CheckUser").value

    BUT STILL HE DOESN'T NEED THAT CHECKBOX.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276
    andreys, I need check box for each record because you need to indicate which order to delete and I use hidden for user login so I assign a temp value for different users. I don't want show real user login in Javascript. Do I use the same way getElementsByName with checkbox too ? I tried to debug it, if it works I will let you all know.
    Tiny Mickey

  8. #8
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Originally posted by learnervb
    andreys, I need check box for each record because you need to indicate which order to delete and I use hidden for user login so I assign a temp value for different users. I don't want show real user login in Javascript. Do I use the same way getElementsByName with checkbox too ? I tried to debug it, if it works I will let you all know.
    Sorry, missed that. Yes, you could use getElementsByName, but by some resons it better works with getElementById.

  9. #9
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    You know, what. Just came to my mind. Why would you just disable/enable CheckBoxDelete check boxes based on login name. In thta case you can't check it, but you'll see it.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276
    andreys, thanks for that good idea. I tried that but I want to promt user (Super) to select any check box or make sure they want to delete before action but my code seems don't go to Javascript funtion. Please help because it's rush. Here is my code
    function chkflds()
    {
    for(var i=0;i<document.getElementById("CheckBoxDelete").length;i++)
    {
    if(document.frmOrderDelete.CheckBoxDelete[i].checked = true)
    {
    var confdel = confirm("Are you sure you want to delete the selected documents?")
    if( confdel)
    {
    document.frmOrderDelete.action = "DeleteOrder.asp";
    document.frmOrderDelete.submit();
    return true;
    } // end if
    } // end if checkbox checked
    else
    {
    alert("No documents selected for deletion!");
    return false;
    } // end else
    }
    }
    <form method="POST" name= "frmOrderDelele" ONSUBMIT="return chkflds()" >
    Tiny Mickey

  11. #11
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    I think OnSubmit event happend before you check your user. That's why it not going to the function. You trigger your function when that event (OnSubmit) already happend.
    You logic should work like this:

    1.Check user
    2.Based on previous Submit form or Not.

    So try to change to (changes in BOLD:
    Code:
    function chkflds() 
    { 
    for(var i=0;i<document.getElementById("CheckBoxDelete").length;i++) 
    { 
    if(document.frmOrderDelete.CheckBoxDelete[i].checked = true) 
    { 
    var confdel = confirm("Are you sure you want to delete the selected documents?") 
    if( confdel) 
    { 
    document.frmOrderDelete.action = "DeleteOrder.asp"; 
    document.frmOrderDelete.submit(); 
    return true; 
    } // end if 
    } // end if checkbox checked 
    else 
    { 
    alert("No documents selected for deletion!"); 
    return false; 
    } // end else 
    } 
    } 
    <form method="POST" name= "frmOrderDelele">  NO EVENTS HERE
    
    <input type="button" name="Delete" value="Delete" OnClick="chkflds()">
    let me know

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276
    andreys, I do nothing at BOLD EVENTS. Here is my complete steps so you will have more ideas what I'mm doing
    On first page (ListOrder.asp)
    <%
    getUser = Session("User")
    Select Case UCase(getUser)
    Case "Super"
    disCheckBox=""
    Case "Normal"
    disCheckBox="disabled"
    End Select
    %>
    <script language= "JavaScript" >
    function chkflds()
    {
    for(var i=0;i<document.getElementById("CheckBoxDelete").length;i++)
    {
    if(document.frmOrderDelete.CheckBoxDelete[i].checked = true)
    {
    var confdel = confirm("Are you sure you want to delete the selected documents?")
    if( confdel)
    {
    document.frmOrderDelete.action = "DeleteOrder.asp";
    document.frmOrderDelete.submit();
    return true;
    } // end if
    } // end if checkbox checked
    else
    {
    alert("No documents selected for deletion!");
    return false;
    } // end else
    }
    }
    </script>
    <form method="POST" name= "frmOrderDelele" onSubmit ="return chkflds() ">
    While loop get data from database
    <td align="center"><font face="Arial">
    <tr><td><input type="checkbox" name="CheckBoxDelete" <%
    =disCheckBox%> id= "CheckBoxDelete" value="<%=rs
    ("OrderID")%>"></td>
    <td align="center" <%=rs("Name")%></td></tr>
    <input type="submit" value="Delete" name="Delete" ></p>
    </HTML>

    On page DeleteOrder.asp
    <%
    getUser = Session("User")
    If getUser = "Super" Then
    SQL = "Delete....."
    End If
    response.Redirect("ListOrder.asp")
    %
    Last edited by learnervb; Jan 20th, 2003 at 04:35 PM.
    Tiny Mickey

  13. #13
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Quick questions: Do you want confirm message box to popup for each selected item? Or one popup for all selected items?

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276
    I just want when user click Delete if no checkbox is checked so should prompt them to check checkbox in order to delete. And if checkbox is checked ( for now I just do delete one by one in DeleteOrder.asp, if you know how to delete multiple at same time, I apreciate if you show me how), then ask them "are you sure to delete" like in Windows explorer. Thanks so much andreys.
    Tiny Mickey

  15. #15
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    OK, here an example that works. Adopt it for your page:
    Code:
    <html>
    <script>
    	function chkflds(){
    		var SelCount=0;
    		//CHECK IF ANY OF 'DELETE' CHECK BOXES ARE SELECTED
    		for(var i=0;i<document.getElementsByName("CheckBoxDelete").length;i++){ 
    			if(document.frmOrderDelete.CheckBoxDelete[i].checked == true){
    				SelCount=SelCount+1;
    				}
    		}
    		//'DELETE' CHECK BOXES ARE SELECTED, AT LEAST ONE
    		if (SelCount>0){
    			//ASK USER IF HE WANT TO DELETE ALL SELECTED ITEMS
    			var conf=confirm("Are you sure you want to delete " + SelCount + " selected documents?");
    			//CONFIRMED, SUBMIT FORM
    			if(conf){ 
    				document.frmOrderDelete.action = "DeleteOrder.asp"; 
    				document.frmOrderDelete.submit(); 
    				}
    			}
    		//NONE OF 'DELETE' CHECK BOXES ARE SELECTED
    		else{
    			alert("No documents selected for deletion!");
    			}
    	} 
    </script>
    <body>
    <form method="POST" name="frmOrderDelete">
      <p>
      <input type="checkbox" name="CheckBoxDelete" value="ON">
      <input type="checkbox" name="CheckBoxDelete" value="ON">
      <input type="checkbox" name="CheckBoxDelete" value="ON">
      <input type="checkbox" name="CheckBoxDelete" value="ON">
      <input type="button" value="Delete" name="Delete" onclick="chkflds()">
      </p>
    </form>
    </body>
    </html>

  16. #16
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Yes, it's possible to delete all of your selected records at once.
    Just give me some time... I'll post an example.

  17. #17
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Here is your ListOrder.asp page
    Code:
    <html>
    <script>
    	function chkflds(){
    		var OrderIDs="";
    		//CHECK IF ANY OF 'DELETE' CHECK BOXES ARE SELECTED
    		for(var i=0;i<document.getElementsByName("CheckBoxDelete").length;i++){ 
    			if(document.frmOrderDelete.CheckBoxDelete[i].checked == true){
    				OrderIDs = OrderIDs + document.frmOrderDelete.CheckBoxDelete[i].value + ",";
    				}
    		}
    		//'DELETE' CHECK BOXES ARE SELECTED, AT LEAST ONE
    		if (OrderIDs!=""){
    			//ASK USER IF HE WANT TO DELETE ALL SELECTED ITEMS
    			var conf=confirm("Are you sure you want to delete selected documents?");
    			//CONFIRMED, SUBMIT FORM
    			if(conf){ 
    				document.frmOrderDelete.action = "DeleteOrder.asp?ordIDs=" + OrderIDs; 
    				document.frmOrderDelete.submit(); 
    				}
    			}
    		//NONE OF 'DELETE' CHECK BOXES ARE SELECTED
    		else{
    			alert("No documents selected for deletion!");
    			}
    	} 
    </script>
    <body>
    <form method="POST" action="" name="frmOrderDelete">
      <p>
      <input type="checkbox" name="CheckBoxDelete" value="1">
      <input type="checkbox" name="CheckBoxDelete" value="2">
      <input type="checkbox" name="CheckBoxDelete" value="3">
      <input type="checkbox" name="CheckBoxDelete" value="4">
      <input type="button" value="Submit" name="Submit" onclick="chkflds()">
      </p>
    </form>
    </body>
    </html>
    DeleteOrder.asp page:

    Code:
    <% 
    getUser = Session("User") 
    If getUser = "Super" Then 
    
    OrderIDs=request("ordIDs")
    
    SQL = "DELETE FROM TableName WHERE OrderID IN(" & OrderIDs & ")" 
    
    End If 
    response.Redirect("ListOrder.asp") 
    %>

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose
    Posts
    276
    Andreys, thank you a ton. Yeah, it worked now. It took me an half day to do this. I can't finish it without your help. Thanks again.
    Last edited by learnervb; Jan 20th, 2003 at 05:11 PM.
    Tiny Mickey

  19. #19
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    You are welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width