Hallo!
Ich habe folgende Form, in welcher mittels JavaScript überprüft wird, ob nicht mehr als eine vorbestimmte anzahl an checkboxen aktiviert ist. Sind mehr als 3 boxen aktiviert. gibt es einen alert(). Ich möchte aber nun, dass auch die form in diesem Fall nicht submitted wird. Da dann ja die Auswahl trotzdem in PHP verarbeitet und ausgegeben wird.
Kann mir da kurz jemand helfen
Ich habe folgende Form, in welcher mittels JavaScript überprüft wird, ob nicht mehr als eine vorbestimmte anzahl an checkboxen aktiviert ist. Sind mehr als 3 boxen aktiviert. gibt es einen alert(). Ich möchte aber nun, dass auch die form in diesem Fall nicht submitted wird. Da dann ja die Auswahl trotzdem in PHP verarbeitet und ausgegeben wird.
PHP:
<?php
if ((isset($submit)) && ($submit == "submit")) {
for ($i = 0; $i < count($_POST['motto']); $i++) {
echo $_POST['motto'][$i];
echo "<br>\n"; } }
?>
<script language=javascript>
function LimitChecks(elementName,allowedQty)
{
var f = document.forms[0];
var e = f.elements[elementName];
var checkedQty = 0;
for(idx=0;idx<e.length;idx++)
{
if(e[idx].checked)
{
checkedQty++;
}
}
if(checkedQty > allowedQty)
{
alert("Du hast "+checkedQty+" Mottos ausgewählt,\n"+
"aber maximal "+allowedQty+" sind erlaubt.\n\n");
}
}
function Validate()
{
LimitChecks("motto[]",3);
}
</script>
<form name="form1" id="form1" method="post" action="test.php">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">
<input name="motto[]" type="checkbox" value="a1" /> a1
<input name="motto[]" type="checkbox" value="a2" /> a2
<input name="motto[]" type="checkbox" value="a3" /> a3 </td>
<td width="50%">
<input name="motto[]" type="checkbox" value="a4" /> a4
<input name="motto[]" type="checkbox" value="a5" /> a5
<input name="motto[]" type="checkbox" value="a6" /> a6
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input name="submit" type="submit" value="submit" onClick="Validate()" /></td>
</tr>
</table>
</form>
Kann mir da kurz jemand helfen