Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-13-2004, 06:22 PM   PM User | #1
mcmuney
New to the CF scene

 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mcmuney is an unknown quantity at this point
Help w/ Drop Down Alert

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?
mcmuney is offline   Reply With Quote
Old 07-13-2004, 06:35 PM   PM User | #2
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
Something like this?

Code:
<!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.
sad69 is offline   Reply With Quote
Old 07-13-2004, 07:11 PM   PM User | #3
mcmuney
New to the CF scene

 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mcmuney is an unknown quantity at this point
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?


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>

Last edited by mcmuney; 07-13-2004 at 07:17 PM..
mcmuney is offline   Reply With Quote
Old 07-13-2004, 07:20 PM   PM User | #4
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
Lots! lol, but no, putting it in a PHP file makes no difference.

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='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.
sad69 is offline   Reply With Quote
Old 07-13-2004, 07:34 PM   PM User | #5
mcmuney
New to the CF scene

 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mcmuney is an unknown quantity at this point
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
mcmuney is offline   Reply With Quote
Old 07-13-2004, 08:47 PM   PM User | #6
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
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.
sad69 is offline   Reply With Quote
Old 07-14-2004, 02:25 AM   PM User | #7
Vincent Puglia
Regular Coder

 
Join Date: Jul 2003
Location: where the World once stood
Posts: 256
Thanks: 0
Thanked 2 Times in 2 Posts
Vincent Puglia is an unknown quantity at this point
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
__________________
mod at WebXperts
Vincent Puglia is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:18 PM.


Advertisement
Log in to turn off these ads.