...

View Full Version : Help w/ Drop Down Alert



mcmuney
07-13-2004, 07:22 PM
I'm looking for a drop-down validator to make sure that a value is selected. For example, if value=none show alert; otherwise, submit. I found a site that has something very similar to what I'm looking for:

http://www.thefixor.com/help/formstyle/dropalert.html

But the only difference is that the code on this specifies it to go to the value if selected. For example, if selected go to http://www.site.com/value and all that I want it to do is submit.

Can someone help?

sad69
07-13-2004, 07:35 PM
Something like this?



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>

<script>
function checkDropDown(selId) {
if(document.getElementById(selId).value != '0') {
return true;
}
else {
alert('Please select an item from the drop down');
return false;
}
}
</script>

</head>
<body>

<form method="post" action="" onsubmit="return checkDropDown('selBox');">
<select name="selBox" id="selBox">
<option value="0">--SELECT ITEM FROM DROP DOWN--</option>
<option value="http://www.yahoo.com">yahoo.com</option>
<option value="http://www.google.com">google.com</option>
<option value="http://www.msn.com">msn.com</option>
</select>
<input type="submit"/>
</form>

</body>
</html>


If you change the name/id of the select, you'll need to change the parameter in the call to checkDropDown(...) in the onsubmit handler.

You may also want to fill in the action attribute of the form as well as give the submit input a name/value.

Hope that helps,
Sadiq.

mcmuney
07-13-2004, 08:11 PM
I made some modifications to it, but it's not working (no alert). I'm using this within a php file, should that make a difference? What's wrong with the code?




<script>
function checkDropDown(shipping) {
if(document.getElementById(shipping).value != 'none') {
return true;
}
else {
alert('Please select an item from the drop down');
return false;
}
}
</script>
<form method='POST' action='checkout2.php' onsubmit='return checkDropDown('shipping');'>
<br>
<select size='1' name='shipping' id='ff' align='left'>
<option selected value=none>Select Shipping</option>
<option value=0.00>UPS Ground-FREE</option>
<option value=10.00>UPS1-$10.00</option>
<option value=20.00>UPS2-$20.00</option>
</select><br><br><input id='submit' type='submit' value='Continue'></form>

sad69
07-13-2004, 08:20 PM
Lots! lol, but no, putting it in a PHP file makes no difference.



<script>
function checkDropDown(shipping) {
if(document.getElementById(shipping).value != 'none') {
return true;
}
else {
alert('Please select an item from the drop down');
return false;
}
}
</script>
<form method='POST' action='checkout2.php' onsubmit='return checkDropDown("shipping");'>
<br>
<select size='1' name='shipping' id='shipping' align='left'>
<option selected value=none>Select Shipping</option>
<option value=0.00>UPS Ground-FREE</option>
<option value=10.00>UPS1-$10.00</option>
<option value=20.00>UPS2-$20.00</option>
</select><br><br><input id='submit' type='button' value='Continue' onClick='validate()'></form>


I've highlighted the changes I made in red. One question I do have concerns validate(). Does this function submit the form? If it does, then it's all good. But if not, then you may still encounter a problem as you're not submitting the form any longer (that submit button is a NOT a submit button, but rather just a regular button).

Hope that helps,
Sadiq.

mcmuney
07-13-2004, 08:34 PM
I actually modified my last post that shows the submit button type='submit' and the validate() removed.

I made the few changes that you showed in red, but in php I can't use ("), they are replaced with ('). Also, I changed the id='shipping'.

Still no alert, HELP!

Thanks,
Sharif

sad69
07-13-2004, 09:47 PM
Why can't you use double quotes? Because that's the problem.

onsubmit='return checkDropDown('shipping');'

If you're using ONLY single quotes, these are the strings you are creating.

Post your PHP code so we can see it..

Sadiq.

Vincent Puglia
07-14-2004, 03:25 AM
function checkDropDown(oForm, selName)
{

var oSel = oForm[selName];
var isOk2Send = false;
for (var i = 0; i < oSel.length; i++)
{
if (oSel.options[i].selected)
isOk2Send = true;
}

if (! isOk2Send)
alert('Please select an item from the drop down');
return (isOk2Send)

}
</script>

</head>
<body>

<form method="post" action="" onsubmit="return checkDropDown(this, 'selBox');">

Vinny



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum