Solved

Javascript Validation for Dropdown list array

Posted on 2004-07-30
6
317 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 100 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Go Fund Me" type plugin 5 44
Google Tag Manager - Add Trigger Using Div class 22 62
WordPress 8 55
Obsolete font tag need to convert to CSS 3 37
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…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

730 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