SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking if at least one checkbox has beeen selected before submitting the form.

    I'm getting a list of records from a database and then putting it in a form where the user can select what they want to update.

    the page's html code is:

    HTML Code:
    html>
    <head>
    <title>Movies 1.00.1 Admin</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript">
    function checkall(state) //checks all checkboxes
    {
    	var n = submit_mult.elements.length;
    	for (i=0; i<n; i++)
    	{
    		if (submit_mult.elements[i].name == "ads[]") submit_mult.elements[i].checked = state;
    	}
    }
    function open_window(link,w,h) //opens new window
    {
    	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
    	wishWin = window.open(link,'wishWin',win);
    }
    </script>
    </head>
    
    <body>
    <table width="100%" border="0" cellspacing="2" cellpadding="2">
    <form method="post" action="" name="submit_mult">
      <tr> 
        <td><strong>ID</strong></td>
        <td><strong>Title</strong></td>
    
        <td><strong>Date</strong></td>
        <td><strong>Expiry</strong></td>
        <td><strong> 
          <input name="checkbox" type="checkbox" onClick="javascript:checkall(this.checked);">
          </strong></td>
      </tr>
      <tr> 
        <td>A45</td>
    
        <td><a href="javascript:open_window('/movies/index.php?view=show&amp;movieid=45&amp;catid=1',400,400);">Jaguar</a></td>
        <td>2006-11-17 19:47:33</td>
        <td>Apr 16, 2007</td>
        <td><input name="ads[]2" type="checkbox" id="ads[]" value="45"></td>
      </tr>
      <tr> 
        <td>A46</td>
    
        <td><a href="javascript:open_window('/movies/index.php?view=show&amp;movieid=46&amp;catid=1',400,400);">The 
          Ghost</a></td>
        <td>2006-11-17 19:51:09</td>
        <td>Feb 14, 2008</td>
        <td><input name="ads[]3" type="checkbox" id="ads[]2" value="46"></td>
      </tr>
      <tr> 
        <td>A47</td>
    
        <td><a href="javascript:open_window('/movies/index.php?view=show&amp;movieid=47&amp;catid=1',400,400);">Catch 
          4</a></td>
        <td>2006-11-17 19:57:33</td>
        <td>Jan 16, 2007</td>
        <td><input name="ads[]4" type="checkbox" id="ads[]3" value="47"></td>
      </tr>
      <tr> 
        <td colspan="5" align="right">With selected: 
          <input name="returned" value="returned" onClick="return(confirm('Update selected posts?'));" type="submit"> 
          <input name="delete" value="delete" onClick="return(confirm('Delete selected posts?'));" type="submit"></td>
    
      </tr>
      </form>
    </table>
    
    </body>
    </html>
    Would someone please help me come up with a javascript to validate if an item has been selected - if not, give an alert 'Please select an item' else give a confirmation box to either return or delete the selected row.

    Thank in advance.

    halfdogg

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Melbourne, Australia
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forms support an onsubmit event so you can create a function that will run through your form and check to see if there is an element with checked = true and if there is return true else return false and the form won't submit. You would probs want to return an error message too. But if this is critical you should do it server side of course.

    so like
    HTML Code:
     <form action="submit.php" onsubmit="javascript:validate();">
     ...
     </form>
    Otherwise you seem to have the javascript heading in the right direction.

    So
    HTML Code:
    ...
    if ( submit_milti.elements[i].checked = true ) {
    valid = true;
    }
    
    if ( valid == true ) {
    return true;
    } else {
    window.alert("oi you didnt check anything you fool!");
    return false;
    }
    sigy


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
  •