i just pasted this code to validate it using the w3c validator but i get a fair amount of errors with the coloured section:
i know if i change it to <option>Please select a gallery</option> and then have the php code below i get the writing first and then my selections after but if the user accidently doesnt select a gallery it would upload the file to the database and give the gallery id=0 which there isnt a gallery which corrosponds to the number 0 so i dont know why it doesnt validate or weather or not i have to have the writing first but then validate it to choose one of the gallerys somehow but i dont know how to validate a dropdown menu
i just pasted this code to validate it using the w3c validator but i get a fair amount of errors with the coloured section:
right, it doesn’t make sense to define option elements inside an option value.
Quote:
Originally Posted by saunders1989
but if the user accidently doesnt select a gallery it would upload the file to the database and give the gallery id=0 which there isnt a gallery which corrosponds to the number 0 so i dont know why it doesnt validate or weather or not i have to have the writing first but then validate it to choose one of the gallerys somehow but i dont know how to validate a dropdown menu
on the PHP side, check for that "unselected" value before processing further. on the client side, you can make a JavaScript, that tells the user he hasn’t selected a gallery and cancel the submit.
If you're attempting to validate the markup that this script outputs, please post the actual markup (HTML source from your browser or from the validator output) so we can evaluate what PHP is actually doing and correct. The validator doesn't care what PHP is doing.
W3 validation has nothing to do with PHP aside from potentially poor form/anchor elements.
If you're asking for how to validate it with php, you'd simply check to see if $_POST['gallery_type'] is an acceptable value with php prior to any insertions into you're database. If not, inform the user and send them back. You can also avoid this by using optgroup with a label in you're selection menu, but since optgroups are non-selectable you have to provide a default gallery to choose from.
As for you're actual HTML building, option cannot have a value of another option. This would be an optgroup:
This will not show in the selection menu for a default option though, an actual gallery reference would be displayed insted - the first one if no selected value is provided.
A solution to this would be to use a list box style instead of the combo box by setting the select size > 1.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
thanks for you replys. could you please explain in a little more detail about the server side about checking if the dropdown menu has been changed to the right one please as i am new to this i dont really understand how?
i did try your optgroup method and thanks for showing me that i never knew about them until you said it. it does seem like a good idea and would work in my case but if i wanted to be strict on validation i would advise the user to select a gallery as i previously had it and then do checks to see if a gallery has been selected.
i did try your optgroup method and thanks for showing me that i never knew about them until you said it. it does seem like a good idea and would work in my case but if i wanted to be strict on validation i would advise the user to select a gallery as i previously had it and then do checks to see if a gallery has been selected.
give the "heading" a value (e.g. 0) and test for that specific value in your script
PHP Code:
if ("0" == $_POST["gallery_type"]) { throw new Exception("No gallery selected."); // die("No gallery selected."); }
EDIT: throwing an Exception is a comfortable way of handling errors. ref. Exception. it allows you to "jump" over code (which is then not executed) until it finds a catch directive, that can handle the error.
simple example
PHP Code:
try { if ("0" == $_POST["gallery_type"]) { throw new Exception("No gallery selected."); } // insert file into DB } catch (Exception $e) { // load the form again with an error message header("Location: upload.php?errmsg=" . urlencode($e->getMessage())); exit; }
note: the most noteable advantage of exceptions is, that they provide you with a lot of information (file/line no. thrown in, time, a stacktrace, etc.)
Last edited by Dormilich; 01-17-2010 at 05:04 PM..
Reason: more about Exception
Exceptions are more useful in an OO approach (or a very well designed chained function handling) to programming over a procedural one. Since you have no idea what code is handling you're classes, exceptions are the easiest way to tell them that invalid, failed, problematic, etc data has been submitted without concerning yourself on the implementation details of the class or object.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
if these dont work with options how do i get a dropdown menu validated then?
i have as my first option just text. saying 'please select a gallery'
the following options are generated from the for statement. but that exception wouldnt work.
so for example
please select a gallery
war - id in database is 1
buildings - id in databse is 2
church - id in database is 3
if i forge to select a gallery my image is uploaded with the id of 0 which will not display it on the correct page.