Solved

Javascript Validation for Dropdown list array

Posted on 2004-07-30
6
268 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
  • 2
6 Comments
 

Expert Comment

by:kevinKohler
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now