ainoy31 Posted May 31, 2007 Share Posted May 31, 2007 I have a drop down menu where my client makes a selection and then click in the select button. The info is pulled from a database. What is the best way to validate that the user chooses a record and then clicks on the select button. I do not want user to just click on the select button without making a selection in the drop down menu. Hopefully this is not confusing. I am using PHP. Much appreciation. AM Quote Link to comment Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 Having the first value of the select box be 0 or something to that effect than on the submitted page you can check the value. That or use javascript to do a check on the value BEFORE submitting. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 JavaScript was designed for this kind of thing. Write a JS script to check that the user has selected something, failing to submit unless a selection has been made. Of course, users can turn off JavaScript or use a browser without it, so you'll also need to check for submitted data in PHP. Just check that $_POST['your_select_name'] is set to some (legal) value. If no value is set, or value is out of range or illegal, redisplay the page with an error message prompting the user to choose something, and include the form again. Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted May 31, 2007 Author Share Posted May 31, 2007 This is what I have: <script text = "text\javascript" language = "javascript"> <!-- function SelectCustomer() { if(document.PendingCustomerInfo.Select.value) { alert("Please select a customer."); return false; } return true; } //--> </script> Do I set the value to something? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 You should be using a not (!) condition (or reverse your conditions). if (! document.PendingCustomerInfo.select.value) { alert("Please select a customer."); return false; } else { return true; } // Or if (document.PendingCustomerInfo.select.value) { return true; } else { alert("Please select a customer."); return false;} What's the onSubmit code in your form? It should be something like: <form .... onSubmit="return SelectCustomer()"> Also, you're supposed to use the "/" slash in "text/javascript." Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted May 31, 2007 Author Share Posted May 31, 2007 That is what I have for the onsubmit = "return SelectCustomer();" I have tried to set the value to NULL, -1...etc Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.