Forum Moderators: open
<script language="JavaScript">
function checkSelects() {
//set a variable to hold the string info
var selectedNo = "";
//test the select boxes for their values
if (document.nameOfForm.NameOfDropDown1.options[document.nameOfForm.NameOfDropDown1.selectedIndex].value!= "yes") {
//If they are not equal to yes then add the text for the alert box of which
//select box is not equal to yes.
selectedNo += "\n - People";
}
if(document.nameOfForm.NameOfDropDown2.options[document.nameOfForm.NameOfDropDown2.selectedIndex].value!= "yes") {
selectedNo += "\n - Places";
}
if (document.nameOfForm.NameOfDropDown3.options[document.nameOfForm.NameOfDropDown3.selectedIndex].value!= "yes") {
selectedNo += "\n - Things";
}
//test to see if you have any string info in the variable
if (selectedNo!= "") {
//If so write an alert box telling which hasn't been selected to yes
selectedNo ="_____________________________\n" +
"You select no for:\n" +
selectedNo + "\n_____________________________" +
//Explain why you don't want these people submiting
"\nWe don't like negative people!\nSo we don't want you to submit\nyour form!";
alert(selectedNo);
//Stop the submit from going through
return false;
}
//No "no"s so let them submit.
else return true;
}
</script>
If you are not looking to tell the user anything then you could always do this:
<script language="JavaScript">
function checkSelects() {
//set a variable to hold the string info
var selectedNo = "";
//test the select boxes for their values
if (document.nameOfForm.NameOfDropDown1.options[document.nameOfForm.NameOfDropDown1.selectedIndex].value!= "yes") {
//If they are not equal to yes then add the text for the alert box of which
//select box is not equal to yes.
selectedNo += "\n - People";
}
if(document.nameOfForm.NameOfDropDown2.options[document.nameOfForm.NameOfDropDown2.selectedIndex].value!= "yes") {
selectedNo += "\n - Places";
}
if (document.nameOfForm.NameOfDropDown3.options[document.nameOfForm.NameOfDropDown3.selectedIndex].value!= "yes") {
selectedNo += "\n - Things";
}
//test to see if you have any string info in the variable
if (selectedNo!= "") {
//Stop the submit from going through
return false;
}
//No "no"s so let them submit.
else return true;
}
</script>
For the form it would look like this. Notice the onSubmit attribute in the opening form tag. It calls the function above. Make sure all names of the form and dropdowns are correct in the above function. You also need a submit button to fire off the onSubmit event.
<form name="nameOfForm" onSubmit="checkSelects()">
Do You Like People?
<select name="NameOfDropDown1">
<option value="no">No</option>
<option value="yes">Yes</option>
</select><br>
Do you like Places?
<select name="NameOfDropDown2">
<option value="no">No</option>
<option value="yes">Yes</option>
</select><br>
Do You Like Things?
<select name="NameOfDropDown3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<br>
<input type="submit">
</form>
BTW Welcome to WebmasterWorld. :)
I really liked the popup that your script gave me when someone answers no. Now I need the people who answer yes to get to go on to the next page. Is that possible?
<form name="nameOfForm" onSubmit="checkSelects()" action="pageorscripthandlingform.cgi">
Then let that page or script do the redirect. Like Oaf357
mentioned serverside scripting will handle this much better than JavaScript.
Make a file ending with .php, put this in the file
<?
phpinfo();
?>
this will check if you have php on your server. If you do then we can help you with a nice php script to handle your problem.