checkbox auswahl begrenzen

tobbimann

Mitglied
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.

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">&nbsp;</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
 
document.form[0].submit.disabled = "true";

Keine Ahnung ob man das so genau aufruft. jedenfalls kann man den Submit Button so ausschalten.
Sieht in HTML so aus:
Code:
<input type="submit" name="submit" value="submit" disabled="true">
 
Der Aufruf stimmt schon, aber wenn der submit Button deaktiviert wird kann die form doch auch wenn die anzahl der aktivierten checkboxen stimmt nicht mehr abgeschickt werden.
 
Die Attributsdefinition disabled="true" gibt es nicht.
Du kannst entweder nach HTML:
[color="2c2c8c"]
&nbsp;&nbsp;<input type="submit" name="submit" value="submit" [color="2c8c2c"]disabled[/color]>
[/color]

oder nach XHTML:
[color="2c2c8c"]
&nbsp;&nbsp;<input type="submit" name="submit" value="submit" [color="2c8c2c"]disabled="disabled" /[/color]>
[/color]

nehmen.
 
Ich kriegs leider nicht hin!

Ich hab jetzt folgeden Code:

PHP:
<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)
{
document.forms[0].submit.disabled = "false";
}
else
{
document.forms[0].submit.disabled = "true";
}
}
function Validate()
{
LimitChecks("motto[]",3);
}
</script>

<form name="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" onClick="Validate()" /> a1 
          <input name="motto[]" type="checkbox" value="a2" onClick="Validate()" /> a2 
          <input name="motto[]" type="checkbox" value="a3" onClick="Validate()" /> a3 </td>
      <td width="50%"> 
          <input name="motto[]" type="checkbox" value="a4" onClick="Validate()" /> a4 
          <input name="motto[]" type="checkbox" value="a5" onClick="Validate()" /> a5 
          <input name="motto[]" type="checkbox" value="a6" onClick="Validate()" /> a6 
		  </td>
    </tr>
    <tr> 
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2"><input name="submit" type="submit" value="submit" disabled/></td>
    </tr>
  </table>
</form>

Es wird jetzt beim anklicken jeder checkbox geprüft, ob die Anzahl zulässig ist. Wenn 3 boxen ausgewählt sind, soll der submit button freigegeben werden.
 
Mal ne ganz blöde Frage. Was zum Geier hat das mit PHP zu tun ? Sind das nicht eher JavaScript Problemchen, die auch in das zugehörige Forum gehören ?
 
Zurück