SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    New Zealand
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tick checkbox and confirm delete

    Hi,

    I'm having problems with the function DeleteRespondents()shown below. What I'm needing to do is get the user to tick a record, submit the form and then get the confirmation prompt.

    The checkbox alert comes up whether ticked or not and the code doesn't progress further. I guess I'm not understanding how to code the true/false, if/then part of this.

    Any help much appreciated

    <script language="JavaScript" type="text/javascript">
    <!--
    function DeleteRespondents()
    {

    if (!Respondents.Delete.checked)
    {
    alert("Please check a delete box");
    return false;
    }

    if (confirm("Are you sure you want to delete the selected respondents?"))
    return false;
    {
    return true;
    document.forms[0].action = "delete.asp?ID=<%=ID%>";
    document.forms[0].submit();

    }
    }

    function UpdateRespondents()
    {
    if (confirm("Are you sure you want to update the records?"))
    {
    document.forms[0].action = "doedit.asp?ID=<%=ID%>";
    document.forms[0].submit();
    }
    }
    // -->
    </SCRIPT>
    JessieMc

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi JessieMc,

    A few things:

    1) if 'Respondents' is the form name, then this:

    if (!Respondents.Delete.checked)
    should read:
    if (!document.Respondents.Delete.checked)

    2) re the following confirm and if/else:

    if(condition) do something is true -- 'OK'
    else do something for false -- 'CANCEL'

    so it should read as follows:

    if (confirm("Are you sure you want to delete the selected respondents?"))
    {
    //process the delete
    document.forms[0].action = "delete.asp?ID=<%=ID%>";
    document.forms[0].submit();
    return true; // this is probably superfluous, but I can't really tell because you didn't post any html
    }
    else
    {
    return false; //no processing
    }

    If you put the return true before the submission, the submit() will never be executed -- because the focus would have left the function.

    Also, I would send the form object as in:
    <input type='button' value='Delete' onClick='DeleteRespondents(this.form)'>

    that way you can have:
    function DeleteRespondents(formObj)
    {
    if (formObj.Delete.checked)
    .....
    formObj.action = "....asp"
    formObj.submit();
    }

    for that matter I would have combined both functions -- function doIt2Respondents(formObj, actionType)
    {
    if (actionType == 1) // update
    .....
    else if (actionType == 0) // delete
    .....
    }

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    New Zealand
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Am I getting close?

    Thanks Vinny,

    Apologies for the delay in replying.


    Have got this far - but am still running into snags - how I wish there was an easy way to debug js!

    Any more help very much appreciated.

    <script language="JavaScript" type="text/javascript">
    <!--
    // form name is frmProcess
    /*
    <INPUT TYPE="BUTTON" CLASS="button2" VALUE="Delete" ONCLICK="doIt2Respondents(frmProcess,1);">
    */

    function doIt2Respondents(formObj, actionType) {

    if (formObj.delete.checked) //checkbox
    alert("Please check a delete box");
    return false;
    }
    else return true;
    }

    if (actionType == 1) // update
    {
    confirm("Are you sure you want to update the records?"));
    formObj.action = "doedit.asp?ID=<%=ID%>"
    formObj.submit();
    }
    else if (actionType == 0) // delete
    {
    confirm("Are you sure you want to delete the records?"));
    formObj.action = "delete.asp?ID=<%=ID%>"
    formObj.submit();
    }
    }
    // -->
    </SCRIPT>
    JessieMc

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    what does your html look like?

    and the Jets best win, else ......

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    New Zealand
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The who?

    Hi Vinny,

    The html is written to the browser in asp - this is the interpreted html.


    <HTML>
    <HEAD>
    <title>List Respondents</title>
    <link rel="stylesheet" type="text/css" href="include/style.css">
    <script language="JavaScript" type="text/javascript">
    <!--
    // form name is frmProcess
    /*
    <INPUT TYPE="BUTTON" CLASS="button2" VALUE="Delete" ONCLICK="doIt2Respondents(frmProcess,1);">
    */

    function doIt2Respondents(formObj, actionType)
    {

    if (actionType == 1) // update
    {
    confirm("Are you sure you want to update the records?"));
    formObj.action = "doeditrespondents.asp?ID=12"
    formObj.submit();
    }
    else if (actionType == 0) // delete
    {
    confirm("Are you sure you want to delete the records?"));
    formObj.action = "delrespondent.asp?ID=12"
    formObj.submit();
    }
    }
    // -->
    </SCRIPT>

    </head><body>
    <div align="center">
    <FORM METHOD=POST ACTION="" NAME="Process"><TABLE CLASS="tablemain" BORDER="0" CELLSPACING="0" CELLPADDING="0" width="600"><tr><td CLASS="tablemain"><TABLE BORDER="0" CELLSPACING="1" CELLPADDING="2" width="100%"><tr><td colspan="4" class="pageheader">Respondents</td></tr><TR><TD class="tableheaderleft">Delete</TD><TD class="tableheaderleft">Email</TD><TD class="tableheaderleft">Group Name</TD><TD class="tableheaderleft">Password</TD></TR>

    <tr><td class="tableleft">
    <INPUT TYPE="CHECKBOX" NAME="delete" CLASS="radiochkbx" VALUE="449">
    <INPUT TYPE="HIDDEN" NAME="0.RespondentID" VALUE="449"></td>

    <td class="tablemiddle">
    <INPUT SIZE=24 TYPE=TEXT NAME="0.Email" VALUE="help@somewhere.com" Class="monospace"></td>

    <td class="tablemiddle">Critters</td><td class="tableright"><INPUT SIZE=15 TYPE=TEXT NAME="0.Password" VALUE="khkh" Class="monospace"></td></tr>

    <tr><td class="sectiondesc" colspan="4" align="right"><br>
    <INPUT TYPE="BUTTON" CLASS="button2" VALUE="Delete" ONCLICK="doIt2Respondents(frmProcess,1);">
    <INPUT TYPE="BUTTON" CLASS="button2" VALUE="Update" ONCLICK="doIt2Respondents(frmProcess,2);">

    <INPUT TYPE="HIDDEN" NAME="Filter" VALUE="Off">
    <INPUT TYPE="HIDDEN" NAME="NoGroup" VALUE="Off">
    <INPUT TYPE="HIDDEN" NAME="iStart" VALUE="0">
    <INPUT TYPE="HIDDEN" NAME="iOffset" VALUE="5">
    <INPUT TYPE="HIDDEN" NAME="Count" VALUE="4"><INPUT TYPE="HIDDEN" NAME="ID" VALUE="12"></td></tr>
    </FORM>
    and so on .........
    JessieMc

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    New Zealand
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still trying with this

    hello,
    I'm still trying to understand this - am now trying "bit by bit".

    Can anyone tell me why this won't submit? Thanks in advance.

    <script language="JavaScript" type="text/javascript">
    <!--
    // form name is frmProcess
    /*
    <INPUT TYPE="BUTTON" CLASS="button2" VALUE="Update" ONCLICK="return(doIt2Respondents(frmProcess,1));">
    */

    function doIt2Respondents(formObj, actionType) {
    if (actionType == 1) { // update
    confirm("Are you sure you want to update the records?");
    return false;
    } else {
    return true;
    formObj.action = "doedit.asp?ID=<%=ID%>"
    formObj.submit();
    }
    }

    // -->
    </SCRIPT>
    JessieMc

  7. #7
    SitePoint Enthusiast RogueJedi's Avatar
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks to me like you've about got it, but it's backwards.

    Try this:
    Code:
    function doIt2Respondents(formObj, actionType) { 
        if (actionType == 1) { // update 
            if(confirm("Are you sure you want to update the records?")) { 
                formObj.action = "doedit.asp?ID=<%=ID%>"; 
                formObj.submit(); 
                return true; 
            } 
            else { 
                return false; 
            } 
        }
        else if (actionType == 0) { // delete
            //Do delete stuff here
        }
    }
    You were doing a return true and then trying to submit the form. That will not work because the function has already exited. Also, remember that "Ok" on a confirm box returns true and "Cancel" returns false, so you had your conditions reversed to start with.

    I have not tested this code, but it should work. If it does not, it should at least get you closer to a solution.

    Robert Nolan
    Do or do not, there is no try.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    New Zealand
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    That's it...

    all now fine....many thanks......so near and yet so far!!
    JessieMc

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    New Zealand
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    final version

    Thought I'd post the final version in case anyone else is looking for something similar.

    Thanks to all involved - I learned heaps.

    <script language="JavaScript" type="text/javascript">
    /*

    <FORM METHOD="Post" ACTION="" NAME="frmProcess">

    <INPUT TYPE="Checkbox" NAME="chkDelete">

    <INPUT TYPE="BUTTON" VALUE="Update" ONCLICK="return(doIt2Respondents(frmProcess,1));">

    <INPUT TYPE="BUTTON" VALUE="Delete" ONCLICK="return(doIt2Respondents(frmProcess,0));">

    */

    function doIt2Respondents(formObj, actionType) {

    if (actionType == 1) { // update
    if(confirm("Are you sure you want to update the records?")) {
    formObj.action = "doedit.asp?ID=<%=ID%>";
    formObj.submit();
    return true;
    }
    else {
    return false;
    }
    }
    else if (actionType == 0) { // delete

    var checkSelected = false; // ensure at least one chkbox is selected
    for (i = 0; i < frmProcess.chkDelete.length; i++)
    {
    if (frmProcess.chkDelete[i].checked)
    checkSelected = true;
    }
    if (!checkSelected)
    {
    alert("Please select at least one checkbox.");
    return (false);
    }
    if(confirm("Are you sure you want to delete the records?")) {
    formObj.action = "delete.asp?ID=<%=ID%>";
    formObj.submit();
    return true;
    }
    else {
    return false;
    }
    }
    }

    // -->
    </SCRIPT>
    JessieMc


Bookmarks

Posting Permissions

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