Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript Validation for Dropdown list array

Posted on 2004-07-30
6
Medium Priority
?
350 Views
Last Modified: 2008-02-20
Hi,

I have used a select box array to capture multiple selected values as :

<select name="food[]" index="food" multiple>
  <option>Ham</option>
  <option>Coke</option>
  <option>Pizza</option>
  <option>Cheese</option>
</select>

This code works fine with PHP, but fails with javascript validation    :(

Any idea how do I do it...

Thanks,
0
Comment
Question by:guifreak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 

Expert Comment

by:kevinKohler
ID: 11677075
Hello,

This is what you need to validation that data.

<script language="JavaScript">
function validatefood() {
Food=theForm.food.selectedIndex;
if (Food=='-1') {
alert('Please Select food');
event.returnValue=false;
}
}
</script>

<form name="theForm">
<select name="food" index="food" multiple>
  <option>Ham</option>
  <option>Coke</option>
  <option>Pizza</option>
  <option>Cheese</option>
</select>
<INPUT type="Submit" value="Submit Change" onclick="validatefood();">
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11678393
You cannot have a select box name be an array.

If you want it in array format then have javascript assemble the array for you although really if you just have a regular variable it will submit as a delimited list and then you can turn it into an array in PHP
0
 
LVL 18

Accepted Solution

by:
arantius earned 400 total points
ID: 11689185
Assign an ID to your select tag:
<select name="food[]" index="food" multiple id="foodSelect">

And use the ID to refer to the element in javascript:
var sel=document.getElementById("foodSelect")
if (sel.selectedIndex ......
0
 
LVL 18

Expert Comment

by:arantius
ID: 11689189
BTW mrichmon: Yes you can have the name be an array.  That's how you tell PHP to take ALL selected values of that element, not just the last one.  And kevin, that's why he needs to keep it that way.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question