473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Checking a dropdown box

3 New Member
Hi everyone,

I have made a script that checks if content is entered in the html boxes and if the required fields are empty, their border lights red.
This all works, but if I try to make a required field of a drop down box I can't get it working -_-
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. .forgotten {
  5.     border: 1px solid #ff0000;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. <?php
  11.     //----- function to check required fields
  12.     function CheckRequired($array)
  13.     {
  14.         //----- check if $array is not empty
  15.         if (!empty($array))
  16.         {
  17.             //----- loop through the array and check if the value's been set
  18.             foreach ($array as $value)
  19.             {
  20.                 if (empty($_POST[$value]))
  21.                     $form_error[] = $value;
  22.             }
  23.         }
  24.  
  25.         //--- return an array with missed fields
  26.         return $form_error;
  27.     }
  28.  
  29.     //----- check for required fields
  30.     if ($_SERVER['REQUEST_METHOD'] == "POST")
  31.         $required_fields = CheckRequired(array('title', 'omschrijving'));
  32.  
  33.     //----- check if all required fields are entered
  34.             if ($_SERVER['REQUEST_METHOD'] == "POST" && empty($required_fields)) 
  35.     {
  36.         echo "Verwerk formulier<br>";
  37.         echo "<u>Categorie:</u> " . $_POST['categorie'] . "<br>";
  38.         echo "<u>Title:</u> " . $_POST['title'] . "<br>";
  39.         echo "<u>Omschrijving:</u> " . $_POST['omschrijving'] . "<br>";
  40.     }
  41.     else
  42.     {
  43.         //----- if form's been posted before, there are some fields left undone
  44.         if (!empty($required_fields))
  45.         {
  46.             $i = 1;
  47.             foreach ($required_fields as $value)
  48.             {
  49.                 // echo "U forgot the field: " . ucfirst($value) . ".<br />";
  50.                 $varname = "class_" . $value;
  51.                 ${$varname} = "class=forgotten";
  52.  
  53.                 $i++;
  54.             }
  55.         }
  56.  
  57.     echo "<form name=\"form\" method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\">";        
  58.     echo "<table>";
  59.     echo "<tr>";
  60.         echo "<td>Categorie:</td>";            
  61.         echo "<td><select name=\"categorie\">";    
  62.              echo "<option value=\"0\">Kies</option>";    
  63.             echo "<optgroup label=\"HTML\">";
  64.             echo "<option value=\"1\">HTML</option>";
  65.             echo "</optgroup>";
  66.  
  67.             echo "<optgroup label=\"CSS\">";
  68.             echo "<option value=\"2\">CSS</option>";
  69.             echo "</optgroup>";
  70.  
  71.             echo "<optgroup label=\"PHP\">";
  72.             echo "<option value=\"3\">PHP</option>";
  73.             echo "<option value=\"4\">Databases</option>";
  74.             echo "<option value=\"5\">OOP</option>";
  75.             echo "</optgroup>";
  76.  
  77.             echo "<optgroup label=\"Design\">";
  78.             echo "<option value=\"6\">Photoshop</option>";
  79.             echo "</optgroup>";            
  80.  
  81.             echo "<optgroup label=\"Programma's\">";
  82.             echo "<option value=\"7\">Programma's</option>";
  83.             echo "</optgroup>";
  84.  
  85.             echo "<optgroup label=\"Besturingssystemen\">";
  86.             echo "<option value=\"8\">Linux</option>";
  87.             echo "<option value=\"9\">Windows</option>";
  88.             echo "<option value=\"10\">Mac</option>";
  89.             echo "</optgroup>";
  90.             echo "</select>";
  91.         echo "</td>";
  92.     echo "</tr>";
  93.     echo "<tr>";
  94.         echo "<td>Titel:</td>";
  95.          echo "<td><input type=\"text\" name=\"title\" width=\"50\" maxlength=\"60\" value=\"" . htmlentities($_POST['title']) . "\" " . $class_title . " /><br></td>";
  96.     echo "</tr>";
  97.     echo "<tr>";
  98.         echo "<td>Omschrijving (inleiding): &nbsp;&nbsp;</td>";
  99.          echo "<td><textarea rows=\"5\" cols=\"50\" name=\"omschrijving\" value=\"" . htmlentities($_POST['omschrijving']) . "\" " . $class_omschrijving . " /></textarea></td>";
  100.     echo "</tr>";
  101.     echo "<tr><td><input type=\"submit\" name=\"submit\" value=\"Verzenden\"><td></tr>";            
  102.      echo "</table>";
  103.     echo "</form>";
  104.  
  105.     }
  106. ?>
  107. </body>
  108. </html>
  109.  
I hope you all can try to help me out, I'm searching a sullution for more then 2 hours.
Greetings,
Reinhout
Aug 14 '07 #1
4 2335
kovik
1,044 Recognized Expert Top Contributor
This all works, but if I try to make a required field of a drop down box I can't get it working -_-
What part doesn't work? Drop down selections aren't like regular text inputs. You can't style them like you can style input boxes, and I'm 90% sure that included borders as well. Also, a drop down selection box always has a default value, so there will always be a posted value. To check if it's been set, you'd need to create a default selection whose value was an empty string, and check it with the empty() function.

Note that I haven't read your code and don't plan to until you are more specific about what the problem is and where it occurs.
Aug 14 '07 #2
Dreea
37 New Member
i am not so sure, but i think this is what is happening :
the function that checks if required fields are filled is checking whether a certain variable is not empty. The variable corresponding to your drop-down list will always have a value(in case nothing is selected, the default value remains selected which has value 0).
hope it helps
Aug 14 '07 #3
reinhout
3 New Member
I have put an example online:
reinhout.online...

The problem is I need to check if the first selection is selected (wich means nothing has changed) and then I need to change the border color.
Now I know I can't change that border :'(

I could give the first selection the value "null" in place of the "0" now and then check it with empty()

I'll try this right now, problems could follow...

edit: I'm looking for something that can replace the red border..
Aug 15 '07 #4
reinhout
3 New Member
I have been looking around and red borders are no big deal with drop-down boxes...
Expand|Select|Wrap|Line Numbers
  1. select { border: 1px solid red; }
Now make a code that fits the border
Aug 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
0
by: Toonman | last post by:
I have a webpage with a <form> consisting of a large table grid of dropdown lists used to make changes in a database. Some of these dropdown lists have the same value. I'm trying to make it so...
6
by: Rey | last post by:
Howdy, all. Appreciate your help. Have a one to many relation between a client and visit table. In the visit subform, I have a visittype and counselor field which are comboboxes. If I set...
13
by: melih.onvural | last post by:
Group, I'm having a tough time understanding some of the previous posts on this topic so I wanted to write what I've tried and hope that you can help me troubleshoot. I have a dropdown populated...
6
by: Neil | last post by:
Hi, I have an aspx page with a number of web controls on it and one of these is a cancel button. I want to check the page to see if the user has changed any of the controls, i..e typed some text...
2
by: 23s | last post by:
I have a dropdown bound to a dataview. The binding assigns the dropdown with a SelectedIndex of 0. There is a msgbox in the SelectedIndexChanged event that displays the SelectedIndex property....
5
by: Gil | last post by:
Is there a way to tell if a combbox is in dropdown mode. I tried and if statement combobox.dropdown = true but i get an error. dropwndown function doesnt store if its true or false what i am...
3
by: devNorway | last post by:
I have been struggling with a problem for days now, and searched for related problems and solutions but had no luck. I have two dropdown listboxes where the first is populated in page load and...
0
by: Kay | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
0
by: Kay O'Keeffe | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.