esport Posted August 8, 2008 Share Posted August 8, 2008 Hi Guys, I am posting a form that consists of an array. <select name="model[]" class='forminput' id="model"> I am trying to do some form validation to check if the user has selected from the drop list. Since the POST value is returned as an array, if there is no value selected, it still picks up a value. How do I determine if the array is empty? I have tried to use the empty function but no luck. Thanks in advance. Daniel Link to comment https://forums.phpfreaks.com/topic/118729-check-arrays/ Share on other sites More sharing options...
vikramjeet.singla Posted August 8, 2008 Share Posted August 8, 2008 you can have check this by isset and count like this: $arr = array(); if(isset($arr) && count($arr) > 0){ // put your logic here } Link to comment https://forums.phpfreaks.com/topic/118729-check-arrays/#findComment-611300 Share on other sites More sharing options...
sKunKbad Posted August 8, 2008 Share Posted August 8, 2008 So if in your form you have selected="selected" set to a null value or placeholder, when the form is processed, you will redirect the user back to the form if the null value or placeholder was posted because it is not in the array of acceptable values. For instance, consider that the value of $model = null: $model_possibilities = array("1","2","3","4","5","6-10","11-15","16+","dont_know"); if(in_array("{$_POST['model']}", $model_possibilities)){ $model = $_POST['model']; }else{ //redirect back to the form... } Link to comment https://forums.phpfreaks.com/topic/118729-check-arrays/#findComment-611330 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.