Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-17-2010, 02:43 PM   PM User | #1
saunders1989
New Coder

 
Join Date: Jan 2010
Posts: 67
Thanks: 12
Thanked 0 Times in 0 Posts
saunders1989 is an unknown quantity at this point
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>
saunders1989 is offline   Reply With Quote
Old 01-17-2010, 04:12 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.
Dormilich is offline   Reply With Quote
Old 01-17-2010, 04:13 PM   PM User | #3
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
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.
bdl is offline   Reply With Quote
Old 01-17-2010, 04:16 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
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
Fou-Lu is offline   Reply With Quote
Old 01-17-2010, 04:41 PM   PM User | #5
saunders1989
New Coder

 
Join Date: Jan 2010
Posts: 67
Thanks: 12
Thanked 0 Times in 0 Posts
saunders1989 is an unknown quantity at this point
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!
saunders1989 is offline   Reply With Quote
Old 01-17-2010, 04:50 PM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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
Dormilich is offline   Reply With Quote
Old 01-17-2010, 10:19 PM   PM User | #7
saunders1989
New Coder

 
Join Date: Jan 2010
Posts: 67
Thanks: 12
Thanked 0 Times in 0 Posts
saunders1989 is an unknown quantity at this point
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
saunders1989 is offline   Reply With Quote
Old 01-18-2010, 06:54 AM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
you mean the Exceptions?
Dormilich is offline   Reply With Quote
Old 01-18-2010, 04:37 PM   PM User | #9
saunders1989
New Coder

 
Join Date: Jan 2010
Posts: 67
Thanks: 12
Thanked 0 Times in 0 Posts
saunders1989 is an unknown quantity at this point
yer the exceptions. not totally sure how it would all work
saunders1989 is offline   Reply With Quote
Old 01-18-2010, 06:24 PM   PM User | #10
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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;

Dormilich is offline   Reply With Quote
Old 01-18-2010, 06:45 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
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
Fou-Lu is offline   Reply With Quote
Old 01-18-2010, 07:10 PM   PM User | #12
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
I just felt like introducing Exceptions …
Dormilich is offline   Reply With Quote
Old 01-18-2010, 07:34 PM   PM User | #13
saunders1989
New Coder

 
Join Date: Jan 2010
Posts: 67
Thanks: 12
Thanked 0 Times in 0 Posts
saunders1989 is an unknown quantity at this point
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.
saunders1989 is offline   Reply With Quote
Old 01-18-2010, 07:48 PM   PM User | #14
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
use Fou-Lu’s code
Dormilich is offline   Reply With Quote
Old 01-18-2010, 08:01 PM   PM User | #15
saunders1989
New Coder

 
Join Date: Jan 2010
Posts: 67
Thanks: 12
Thanked 0 Times in 0 Posts
saunders1989 is an unknown quantity at this point
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)
{
... 
saunders1989 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:42 AM.


Advertisement
Log in to turn off these ads.