Jump to content

Detect Checkboxes


)(Gravitek

Recommended Posts

Hi there,

I'm wanting to use a javascript form validation that alerts the user if none of the available checkboxes have been checked. I've done some surfing to try to find a solution, but I think the recommendations aren't working becuase of the way I've created the checkboxes...

 

the following is the line of PHP code i use to display the checkbox in a table. The table is created from a loop because it obtains the information from a MySQL database (hence the use of echo):

echo '\t<TD><CENTER><INPUT TYPE="CHECKBOX" NAME="del[]" VALUE=".$row['id']."></TD>\n';

 

After submission of the form, PHP deletes the record out of the MySQL database, using $row['id'] as the identifier, which is why i've used del[] as the NAME of the input type:

 

if ($_POST['delete']) {

    $del = $_POST['del'];

    foreach ($del as $array) {

      $delsql = "DELETE FROM spares WHERE id='$array';";

      mysql_query($delsql);

      echo "Record number ".$array." deleted!<BR>";

    }

}

 

---- from here on is where I am stuck ----

 

The idea I had to validate the form was to use a function. The function would be called when the submit button is pressed like this (the table NAME is deletetable):

<INPUT TYPE="button" NAME="delete" onclick="validateCheckboxes()">

 

The function looks like this (which isnt working)

 

function validateCheckboxes() {

var formObj = document.deletetable;

    if (formObj.del.length == "0") {

      alert("Please select the records you wish to delete and try again!");

      return false;

    } else {

      formObj.submit;

    }

}

 

 

Any help would be greatly appreciated!!!!!

Link to comment
https://forums.phpfreaks.com/topic/39278-detect-checkboxes/
Share on other sites

Thanks for the reply. I must be doing something wrong still however, because I get syntax error on line 3...

 

function valIssue() {

var formObj=document.deletetable;

var x=formObj.del[].length;

for (var y = -1; y <= x; y++) {

    if (formObj.del[y].checked = TRUE) {

        var submit = "1";

    }

}

if (submit == "1") {

    formObj.submit;

} else {

    alert("Please select part(s) to delete!");

}

}

Link to comment
https://forums.phpfreaks.com/topic/39278-detect-checkboxes/#findComment-189887
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.