Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts

    dropdown menu issue

    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

    thanks

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <?php
    
    
    $dbLink = new mysqli('localhost', 'root', '', 'gallery');
             if(mysqli_connect_errno()) {
                 die("MySQL connection failed: ". mysqli_connect_error());
             }
    
    $query = "SELECT gallery_type_id, gallery_type_desc FROM gallery_type ORDER BY gallery_type_id";
    
    /* run the query */
    $result = $dbLink->query($query);
    
    /* load the query's results into an array */
    $i=1;
    while($row=$result->fetch_assoc()){
    $galleryTypes[$i] = $row;
    $i++;
    }  
    $num_results = $result->num_rows;
    ?>
    
    </head>
    
    <body>
    
     <form action="add_file.php" method="post" enctype="multipart/form-data">
     		 <input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
             <input type="file" name="uploaded_file" /><br />
             <input type="submit" value="Upload file" />  
    		 <label for="gallery_type">Gallery Type</label>
    		 <select name="gallery_type" id="gallery_type">
            	<option value="<?php
    				for($i = 1; $i < $num_results+1; $i++) {
    				echo "<option value=\"" . $galleryTypes[$i]['gallery_type_id'] . "\">";
    				echo $galleryTypes[$i]['gallery_type_desc'];
    				echo "</option>\n";
    				}
    			?>"
                </option>
    		</select>
    	</form>
    
    </body>
    </html>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by saunders1989 View Post
    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 View Post
    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.

  • #3
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
                <optgroup label="Please Select">
    <?php
                    
    for($i 1$i $num_results+1$i++) {
                    echo 
    "<option value=\"" $galleryTypes[$i]['gallery_type_id'] . "\">";
                    echo 
    $galleryTypes[$i]['gallery_type_desc'];
                    echo 
    "</option>\n";
                    }
                
    ?>"
                </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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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.

    could you please advise how to go about that

    Thanks!

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by saunders1989 View Post
    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

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    hi. i dont understand how that fits around my code
    PHP Code:
    <?php
                    
    for($i 1$i $num_results+1$i++) {
                    echo 
    "<option value=\"" $galleryTypes[$i]['gallery_type_id'] . "\">";
                    echo 
    $galleryTypes[$i]['gallery_type_desc'];
                    echo 
    "</option>\n";
                    }
                
    ?>
    i am very new to php so please could you explain quite simple how i would fit it around it

    thanks

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    you mean the Exceptions?

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    yer the exceptions. not totally sure how it would all work

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    the Exceptions are not meant to work with the <option> code. it’s meant to aid you when testing whether a valid gallery type has been submitted.

    PHP Code:
    try
    {
        if (
    "0" == $_POST["gallery_type"])
        {
            throw new 
    Exception("No gallery selected.");
        }
        
    // connect to DB
        
    for ($i 1$i <= $num_results$i++) 
        {
            echo 
    "<option value=\""$galleryTypes[$i]['gallery_type_id'], "\">";
            echo 
    $galleryTypes[$i]['gallery_type_desc'];
            echo 
    "</option>\n";
        }
    }
    catch (
    Exception $e)
    {
        
    header("Location: upload.php?errmsg=" urlencode($e->getMessage()));
        exit;


  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This being said, exceptions are of little use in a procedural fashion. Its silly to throw and catch when you can simply validate:
    PHP Code:
    if (== $_POST['gallery_type'])
    {
        
    header('Location: http://mysite.com/upload.php?errmsg=' urlencode('No gallery selected'));
        exit;
    }

    for (
    $i 0<= $num_results; ++$i)
    {
    ... 
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #12
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    I just felt like introducing Exceptions …

  • #13
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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.

  • #14
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    use Fou-Lu’s code

  • #15
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i tried that but i dont understand the header line.

    the website address is 'http://localhost/Blean_Photos/upload.php'

    and if i put this code. i dont get the .urlencode part. i dont think it would work

    PHP Code:
    if (== $_POST['gallery_type'])
    {
        
    header('Location: 'http://localhost/Blean_Photos/upload.php' . urlencode('No gallery selected'));
        
    exit;
    }

    for (
    $i 0<= $num_results; ++$i)
    {
    ... 


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •