PDA

View Full Version : Checking if form empty


chesneil
01-29-2008, 01:25 AM
Hi,
I have a form with a number of checkboxes and a submit button. Is there a simple way to check if any of the buttons are ticked when the user clicks submit? Something better than if(!isset(checkbox A) && !isset(checkbox B)...){echo "nothing completed!";} kind of thing. I'm looking for something that checks the form to see if there's anything inside it except $submit="Submit".

Thanks.

fl00d
01-29-2008, 02:09 AM
Err I think a Javascript or AJAX solution is what you're looking for then.

JohnDubya
01-29-2008, 02:40 PM
chesneil, I've moved your topic to Javascript, since that's what you'll be needing to do this with your form. You should be able to write a simple function that will see if those checkboxes are checked or not. Like:


if (document.form_name.input_name.checked) {
//do this
} else {
//if checkbox isn't checked, do this
}


Others can help you further. Good luck! :thumbsup:

Philip M
01-29-2008, 04:00 PM
You don't give much information, but you want something like:-


<form name = "myform">
<input type = "checkbox" name = "chk1" value = "Red">RED<br>
<input type = "checkbox" name = "chk2" value = "Blue">BLUE<br>
<input type = "checkbox" name = "chk3" value = "Green">GREEN<br>
<input type = "button" value = "Poll" onclick = "poll()"><br>
</form>

<script type = "text/javascript">

function poll() {
var count = 0;
var res = "Your colour choice is ";
for (var i = 1; i<4; i++) {
if (eval("document.myform.chk" + i + ".checked")) {
res += eval("document.myform.chk" + i + ".value") + " ";
count ++;
}
}
if (count == 0) {
res = "No Choice Made"
}
alert (res);
}

</script>

chesneil
01-30-2008, 01:06 AM
Well, there wasn't much info to give. What I was after was something like this: You know how you can set a Clear button that clears all values in that form? I was wondering if there was a special function that would clear ALL forms in the document, and that could be called in a <body onLoad=> function.

I just ended up calling a function full of 'document.certainform.box1.value="";' lines in a body onLoad.

Thanks for your help, guys. This place is great. :)

glenngv
01-30-2008, 06:16 PM
<body onload="document.certainform.reset();">

chesneil
01-31-2008, 02:02 AM
That's a good one. Can I get it to work if the viewer uses the back button on their browser?

glenngv
01-31-2008, 03:01 PM
Everytime the page loads, the form will be reset.

chesneil
01-31-2008, 03:17 PM
It doesn't work on checkboxes when using the browser back arrow or history.go(-1)

glenngv
01-31-2008, 04:02 PM
I tried this simple form and it works.
<html>
<body onload="document.forms[0].reset();">
<form>
<input type="text" value="textbox default" />
<input type="checkbox" checked="checked" />Uncheck it
<textarea>textarea default</textarea>
<select>
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
</select>
</form>
<a href="http://www.google.com">Edit fields above, click link then go back to this page</a>
</body>
</html>

chesneil
01-31-2008, 11:21 PM
Sorry, but it doesn't reset when I click the link and use the back arrow. Is this a Mac/Safari thing?

PS Doesn't work in Mac/Firefox either.

glenngv
02-01-2008, 09:06 PM
I only tried it in IE. That's the only allowed browser at my work.