Jump to content

How come This doesn't work???


rn14

Recommended Posts

I have 7 check boxes of which 5 and only 5 can be selected. These are printed in an array.

 

These are my functions:

 

<script type="text/javascript">
function checkArray(mainform, arrayName) { var retval = new Array(); for(var i=0; i < form.elements.length; i++) { var el = form.elements[i]; if(el.type == "checkbox" && el.name == arrayName && el.checked) { retval.push(el.value); } } return retval; } 

function checkForm(mainform) { var itemsChecked = checkArray(mainform, "meal[]"); alert("You selected " + itemsChecked.length + " items"); if(itemsChecked.length > 0) { alert("The items selected were:\n\t" + itemsChecked); } return false; } 
</script>

 

This is the form that i use

 


<form method="post" action = "our_menustestw2.php" name = "mainform" onsubmit="checkForm();" >


<input type="checkbox" name="meal2[]"  value="<?php echo $row['id']; ?>" CHECKED/> 

<input type="submit" value="Submit Changes" name = "b1"> 


 

Thanks in advance for any help

Link to comment
Share on other sites

assuming your form just didn't copy wrong...i found a couple other issues.

 

#1) In the form tag, to prevent the form from posting, you need to RETURN the return value

#2) In checkArray, you start with mainForm, but then start using form instead

#3) In checkForm, you use 'meal[]', but in your form, it is 'meal2[]'

 

updated code that works:

<script type="text/javascript">
function checkArray(mainform, arrayName){
  var retval = new Array();
  for(var i=0; i < mainform.elements.length; i++){
    var el = mainform.elements[i];
    if(el.type == "checkbox" && el.name == arrayName && el.checked){
      retval.push(el.value);
    }
  }
  return retval;
}

function checkForm(mainform){
  var itemsChecked = checkArray(mainform, "meal[]");
  alert("You selected " + itemsChecked.length + " items");
  if(itemsChecked.length > 0) {
    alert("The items selected were:\n\t" + itemsChecked);
  }
  return false;
}
</script>

<form method="post" action = "our_menustestw2.php" name = "mainform" onsubmit="return checkForm(this);" >
  <input type="checkbox" name="meal[]"  value="1" />
  <input type="checkbox" name="meal[]"  value="2" />
  <input type="checkbox" name="meal[]"  value="3" />
  <input type="checkbox" name="meal[]"  value="4" />
  <input type="checkbox" name="meal[]"  value="5" />
  <input type="checkbox" name="meal[]"  value="6" />
  <input type="checkbox" name="meal[]"  value="7" />

  <input type="submit" value="Submit Changes" name = "b1">
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.